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/export meals #329

Merged
merged 2 commits into from
Dec 10, 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
17 changes: 9 additions & 8 deletions app/Domains/Meal/Models/Meal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace App\Domains\Meal\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Insane\Journal\Models\Product\Product;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Meal extends Model
{
Expand All @@ -26,11 +26,12 @@ public function saveIngredients($items)
{
Ingredient::query()->where('meal_id', $this->id)->delete();
foreach ($items as $item) {
if (isset($item['product_id']) && $item['product_id'] !== "new::{$item['name']}") {
if (isset($item['product_id']) && !str_contains($item['product_id'], "new::")) {
$product = Product::find($item['product_id']);
} elseif (isset($item['name'])) {
$product = Product::create([
'name' => $item['name'],
} else {
$ingredientName = str_replace("new::", "", $item['product_id']);
$product = Product::firstOrCreate([
'name' => $ingredientName,
'team_id' => $this->team_id,
'user_id' => $this->user_id,
]);
Expand All @@ -40,9 +41,9 @@ public function saveIngredients($items)
'user_id' => $this->user_id,
'meal_id' => $this->id,
'product_id' => $product->id,
'quantity' => $item['quantity'],
'name' => $item['name'],
'unit' => $item['unit'],
'quantity' => $item['quantity'] ?? 1,
'name' => $product->name,
'unit' => $item['unit'] ?? "",
]);
}
}
Expand Down
27 changes: 23 additions & 4 deletions app/Domains/Meal/Services/MealService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

namespace App\Domains\Meal\Services;

use App\Domains\AppCore\Models\Planner;
use App\Models\User;
use Illuminate\Support\Carbon;
use App\Domains\Meal\Models\Meal;
use App\Domains\Meal\Models\Product;
use App\Domains\Meal\Models\MealPlan;
use Illuminate\Support\Carbon;
use App\Domains\AppCore\Models\Planner;

class MealService
{
public function addPlan($mealsData)
{
foreach ($mealsData['meals'] as $mealData) {
if (isset($mealData['id']) && $mealData['id'] !== "new::{$mealData['name']}") {
if (isset($mealData['id']) && !str_contains($mealData['id'],"new::")) {
$meal = Meal::find($mealData['id']);
} elseif (isset($mealData['name'])) {
$meal = Meal::create([
'name' => $mealData['name'],
'name' => str_replace("new::", "", $mealData['name']),
'meal_type_id' => $mealData['meal_type_id'],
'team_id' => $mealsData['team_id'],
'user_id' => $mealsData['user_id'],
Expand Down Expand Up @@ -106,4 +108,21 @@ public static function getIngredients($plans)

return $ingredients;
}

public function addIngredientLabel(Product $product, mixed $postData, User $user) {
if (!str_contains($postData['label_id'], "new::")) {
$label = $product->labels()->find($postData['label_id']);
} else {
$labelName = str_replace("new::", "", $postData['label_id']);
$label = $product->labels()->create([
'user_id' => $user->id,
'team_id' => $user->current_team_id,
'name' => $labelName,
'label' => $labelName,
'color' => $postData['color'] ?? "",
]);
}

return $label;
}
}
22 changes: 8 additions & 14 deletions app/Http/Controllers/Api/IngredientApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Api;

use App\Domains\Meal\Models\Product;
use App\Domains\Meal\Services\MealService;

class IngredientApiController extends BaseController
{
Expand All @@ -14,21 +15,14 @@ public function __construct()
$this->includes = ['labels'];
}

public function addLabel($id)
public function addLabel($id, MealService $mealService)
{
$postData = request()->post();
$product = $this->model->find($id);
if ($postData['label_id'] !== "new::{$postData['name']}") {
$label = $product->labels()->find($postData['label_id']);
} else {
$label = $product->labels()->create([
'user_id' => auth()->user()->id,
'team_id' => auth()->user()->current_team_id,
'name' => $postData['name'],
'label' => $postData['name'],
'color' => $postData['color'],
]);
}
$product = $this->model->find($id);
$label = $mealService->addIngredientLabel(
$product,
request()->post(),
request()->user()
);

return response()->json(['label' => $label]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public function up(): void
{
Schema::table('budget_months', function (Blueprint $table) {
$table->decimal('accounts_balance')->default(0)->after('left_from_last_month');
$table->decimal('overspending_from_last_month')->default(0);
$table->json('meta_data')->nullable();
});
}
};
8 changes: 1 addition & 7 deletions resources/js/Components/icons/IconBell.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@

<template>
<svg width="1em" height="1em" viewBox="0 0 24 24"><path fill="currentColor" d="M20 17h2v2H2v-2h2v-7a8 8 0 1 1 16 0v7zm-2 0v-7a6 6 0 1 0-12 0v7h12zm-9 4h6v2H9v-2z"></path></svg>
</template>

<script>
export default {
name: 'RiNotification3Line'
}
</script>
</template>
24 changes: 12 additions & 12 deletions resources/js/Components/molecules/AppNotificationBell.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
<template>
<LogerButtonCircle
type="button"
>
<IconBell class="text-md" />
<div v-if="notifications > 0" class="absolute bottom-0 right-0 w-4 h-4 text-xs text-white bg-error rounded-full shadow-md">
{{ notifications }}
</div>
</LogerButtonCircle>
</template>

<script setup>
<script setup lang="ts">
import LogerButtonCircle from '@/Components/atoms/LogerButtonCircle.vue';
import IconBell from '@/Components/icons/IconBell.vue';

Expand All @@ -23,3 +12,14 @@ defineProps({
})

</script>

<template>
<LogerButtonCircle
type="button"
>
<IconBell class="text-md" />
<div v-if="notifications > 0" class="absolute bottom-0 right-0 w-4 h-4 text-xs text-white rounded-full shadow-md bg-error">
{{ notifications }}
</div>
</LogerButtonCircle>
</template>
61 changes: 5 additions & 56 deletions resources/js/Pages/Finance/Account.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import TransactionSearch from "@/domains/transactions/components/TransactionSear
import TransactionTable from "@/domains/transactions/components/TransactionTable.vue";
import DraftButtons from "@/domains/transactions/components/DraftButtons.vue";

import { useTransactionModal, TRANSACTION_DIRECTIONS } from "@/domains/transactions";
import { useTransactionModal, TRANSACTION_DIRECTIONS, removeTransaction } from "@/domains/transactions";
// import { IServerSearchData, useServerSearch } from "@/composables/useServerSearch";
import { tableAccountCols } from "@/domains/transactions";
import { useAppContextStore } from "@/store";
import { formatMoney } from "@/utils";
import { IAccount, ICategory, ITransaction } from "@/domains/transactions/models";
import axios from "axios";
import AccountReconciliationForm from "./AccountReconciliationForm.vue";


const { openTransactionModal } = useTransactionModal();
Expand Down Expand Up @@ -260,63 +261,11 @@ const reconcileForm = useForm({
/>
</section>

<ConfirmationModal
<AccountReconciliationForm
:show="reconcileForm.isVisible"
@close="reconcileForm.isVisible = false"
title="Ending statement balance"
>

<template #content>
<section>
<h4 class="font-bold">
{{ selectedAccount.name }}
</h4>
<AtField
label="Ending balance Date"
class="flex justify-between w-full md:w-4/12 md:block"
>
<NDatePicker
v-model:value="reconcileForm.date"
type="date"
size="large"
class="w-48 md:w-full"
/>
</AtField>

<AtField label="statement balance">
<LogerInput
ref="input"
class="opacity-100 cursor-text"
v-model="reconcileForm.balance"
:number-format="true"

>
<template #prefix>
{{ selectedAccount.currency_code }}
</template>
</LogerInput>
</AtField>
</section>

</template>

<template #footer>
<section class="flex justify-between">
<LogerButton @click="reconcileForm.isVisible = false" variant="neutral">
Cancel
</LogerButton>

<LogerButton
class="ml-2"
@click="reconciliation"
:class="{ 'opacity-25': reconcileForm.processing }"
:disabled="reconcileForm.processing"
>
Save
</LogerButton>
</section>
</template>
</ConfirmationModal>
:account="selectedAccount"
/>
</FinanceTemplate>
</AppLayout>
</template>
134 changes: 134 additions & 0 deletions resources/js/Pages/Finance/AccountReconciliationForm.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<script setup lang="ts">
import { useForm } from "@inertiajs/vue3";
import { format } from "date-fns";
import { NDatePicker } from "naive-ui";
import { AtField, } from "atmosphere-ui";


import LogerButton from "@/Components/atoms/LogerButton.vue";
import ConfirmationModal from "@/Components/atoms/ConfirmationModal.vue";
import LogerInput from "@/Components/atoms/LogerInput.vue";

import { IAccount } from "@/domains/transactions/models";
import { formatMoney } from "@/utils";

const emit = defineEmits(['close']);
const props = withDefaults(defineProps<{
isVisible: boolean;
account: IAccount,
}>(), {});

// reconciliation
const reconcileForm = useForm({
isVisible: false,
date: new Date(),
balance: 0,
hasDifference: false,
})

const onClose = () => {
reconcileForm.reset()
emit('close')
}

const reconciliation = () => {
reconcileForm.transform(data => ({
...data,
date: format(data.date, 'yyyy-MM-dd'),
})).post(`/finance/reconciliation/accounts/${props.account.id}`, {
preserveScroll: true,
only: ['transactions', 'accounts', 'stats'],
onFinish() {
onClose()
}
});
};

const doQuickReconciliation = () => {
reconcileForm.balance = props.account.balance;
reconciliation()
}


</script>

<template>
<ConfirmationModal
:show="isVisible"
@close="onClose"
:max-width="reconcileForm.hasDifference ? 'md' : 'sm'"
title="Ending statement balance"
>

<template #content>
<article v-if="!reconcileForm.hasDifference">
<h4>Is your current account balance</h4>
<h2 class="text-lg"> {{ formatMoney(account.balance) }} </h2>
<footer class="flex justify-end">
<LogerButton @click="reconcileForm.hasDifference = true" variant="neutral">
No
</LogerButton>

<LogerButton
class="ml-2"
@click="doQuickReconciliation"
:class="{ 'opacity-25': reconcileForm.processing }"
:disabled="reconcileForm.processing"
>
Yes
</LogerButton>
</footer>
</article>
<section v-else>
<h4 class="font-bold">
{{ account.name }}
</h4>
<AtField
label="Ending balance Date"
class="flex justify-between w-full md:block"
>

<NDatePicker
v-model:value="reconcileForm.date"
type="date"
size="large"
class="w-full"
/>
</AtField>

<AtField label="statement balance">
<LogerInput
ref="input"
class="opacity-100 cursor-text"
v-model="reconcileForm.balance"
:number-format="true"

>
<template #prefix>
{{ account.currency_code }}
</template>
</LogerInput>
</AtField>
</section>

</template>

<template #footer v-if="reconcileForm.hasDifference">
<section class="flex justify-between">
<LogerButton @click="onClose" variant="neutral">
Cancel
</LogerButton>

<LogerButton
class="ml-2"
@click="reconciliation"
:class="{ 'opacity-25': reconcileForm.processing }"
:disabled="reconcileForm.processing"
>
Save
</LogerButton>
</section>
</template>
</ConfirmationModal>

</template>
Loading