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

feat(restock): Add a delete and view button to delete and see restock #82

Merged
merged 2 commits into from
May 1, 2024
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
26 changes: 24 additions & 2 deletions backend/internal/db/mongo/restock_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@ func (b *Backend) GetAllRestocks(ctx context.Context, page uint64, size uint64)
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()

filter := bson.M{}
filter := bson.M{
"$or": []bson.M{
{
"deleted_at": bson.M{
"$exists": false,
},
},
{
"deleted_at": nil,
},
},
}

// Get "size" restocks from "page" using aggregation
var restocks []*models.Restock
Expand All @@ -72,7 +83,18 @@ func (b *Backend) CountAllRestocks(ctx context.Context) (uint64, error) {
ctx, cancel := b.TimeoutContext(ctx)
defer cancel()

filter := bson.M{}
filter := bson.M{
"$or": []bson.M{
{
"deleted_at": bson.M{
"$exists": false,
},
},
{
"deleted_at": nil,
},
},
}

count, err := b.db.Collection(RestocksCollection).CountDocuments(ctx, filter)
if err != nil {
Expand Down
233 changes: 222 additions & 11 deletions frontend/src/routes/panel/products/reappro/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<script lang="ts">
import type { Item, NewRestock, NewRestockItem, Restock } from '$lib/api';
import ConfirmationPopup from '$lib/components/confirmationPopup.svelte';
import { itemsApi, restocksApi } from '$lib/requests/requests';
import { formatPrice, parsePrice } from '$lib/utils';
import { onMount } from 'svelte';

let sure: boolean = false;
let items: Item[] = [];

let restoks: Restock[] = [];
let restocks: Restock[] = [];
let newRestock: NewRestock = {
total_cost_ht: 0,
total_cost_ttc: 0,
Expand Down Expand Up @@ -56,14 +57,20 @@
bundle_cost_ttc: "Prix d'un lot TTC"
};

let deletingRestock = false;
let deleteRestockCallback: VoidFunction = () => {};
let confirmationMessage: string | undefined = undefined;

let selectedRestock: Restock | undefined = undefined;

onMount(() => {
reloadItems();
restocksApi()
.getRestocks(page, itemsPerPage, undefined, undefined, {
withCredentials: true
})
.then((res) => {
restoks = res.data.restocks ?? [];
restocks = res.data.restocks ?? [];
});
});

Expand All @@ -90,7 +97,8 @@
if (item.bundle_cost_float_ttc === 0.0) {
newRestock.total_cost_ttc += item.amount_of_bundle * item.bundle_cost_ttc;
} else {
newRestock.total_cost_ttc += item.amount_of_bundle * (item.bundle_cost_float_ttc ?? item.bundle_cost_ttc);
newRestock.total_cost_ttc +=
item.amount_of_bundle * (item.bundle_cost_float_ttc ?? item.bundle_cost_ttc);
}
});
}
Expand All @@ -103,7 +111,7 @@
restocksApi()
.createRestock(newRestock, { withCredentials: true })
.then((res) => {
restoks = [res.data, ...restoks];
restocks = [res.data, ...restocks];
newRestock = {
total_cost_ht: 0,
total_cost_ttc: 0,
Expand Down Expand Up @@ -131,8 +139,7 @@
bundle_cost_float_ttc: 0.0,
tva: 0
};
// @ts-ignore
document.getElementById('CHECKBOX').checked = false;
sure = false;
});
}

Expand All @@ -159,6 +166,14 @@
displayedValues.item_price_ht = formatPrice(newItem.bundle_cost_ht * newItem.amount_of_bundle);
displayedValues.item_price = formatPrice(newItem.bundle_cost_ttc * newItem.amount_of_bundle);
}

function deleteRestock(restockId: string) {
restocksApi()
.deleteRestock(restockId, { withCredentials: true })
.then(() => {
restocks = restocks.filter((ct) => ct.id !== restockId);
});
}
</script>

<div class="max-w-[95%] px-4 py-10 sm:px-6 lg:px-8 lg:py-14 mx-auto">
Expand Down Expand Up @@ -605,15 +620,33 @@
Prix total TTC
</p>
</th>
<th scope="col" class="px-2 py-3">
<p
class="text-center text-xs font-semibold uppercase tracking-wide text-gray-800 dark:text-gray-200"
>
Actions
</p>
</th>
</tr>
{#each restoks as restok}

{#if deletingRestock}
<ConfirmationPopup
message={confirmationMessage}
confirm_text="Supprimer"
cancel_callback={() => {
deletingRestock = false;
}}
confirm_callback={deleteRestockCallback}
/>
{/if}
{#each restocks as restock}
<tr>
<td class="px-6 py-3">
<div class="flex flex-col">
<div
class="rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-gray-300 text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent"
>
<p>{restok.created_at}</p>
<p>{restock.created_at}</p>
</div>
</div>
</td>
Expand All @@ -622,7 +655,7 @@
<div
class="rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-gray-300 text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent"
>
<p>{restok.type}</p>
<p>{restock.type}</p>
</div>
</div>
</td>
Expand All @@ -631,19 +664,197 @@
<div
class="rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-gray-300 text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent"
>
<p>{restok.created_by_name}</p>
<p>{restock.created_by_name}</p>
</div>
</div>
</td>
<td class="px-2 py-3">
<p
class="text-center text-xs font-semibold uppercase tracking-wide text-gray-800 dark:text-gray-200"
>
{formatPrice(restok.total_cost_ttc)}
{formatPrice(restock.total_cost_ttc)}
</p>
</td>
<td class="px-2 py-3">
<div
class="text-center text-xs font-semibold uppercase tracking-wide text-gray-800 dark:text-gray-200"
>
<button
class="px-2 inline-flex items-center text-sm text-blue-600 decoration-2 hover:underline font-medium"
on:click={() => {
deleteRestockCallback = () => {
deletingRestock = false;
deleteRestock(restock.id);
};
confirmationMessage =
'Supprimer la réappro de ' +
restock.created_by_name +
' à ' +
restock.type +
' ?';
deletingRestock = true;
}}
>
Supprimer
</button>
<button
class="px-2 inline-flex items-center text-sm text-blue-600 decoration-2 hover:underline font-medium"
on:click={() => {
if (selectedRestock == restock) {
selectedRestock = undefined;
} else {
selectedRestock = restock;
}
}}
>
Voir
</button>
</div>
</td>
</tr>
{/each}
</thead>
</table>
{#if selectedRestock != undefined}
<table class="mb-10 min-w-full divide-y divide-gray-200 dark:divide-gray-700 bg-blue-950">
<thead class="bg-gray-50 dark:bg-blue-600">
<tr>
<th scope="col" class="px-12 py-3">
<span
class="text-center text-xs font-semibold uppercase tracking-wide text-gray-800 dark:text-gray-200"
>
Nom
</span>
</th>
<th scope="col" class="px-3 py-3 w-48">
<span
class="text-center text-xs font-semibold uppercase tracking-wide text-gray-800 dark:text-gray-200"
>
Prix coûtant HT
</span>
</th>
<th scope="col" class="px-3 py-3 w-48">
<span
class="text-center text-xs font-semibold uppercase tracking-wide text-gray-800 dark:text-gray-200"
>
Prix coûtant TTC
</span>
</th>
<th scope="col" class="px-3 py-3">
<span
class="text-center text-xs font-semibold uppercase tracking-wide text-gray-800 dark:text-gray-200"
>
Nombre de lots
</span>
</th>
<th scope="col" class="px-3 py-3">
<span
class="text-center text-xs font-semibold uppercase tracking-wide text-gray-800 dark:text-gray-200"
>
Nbr produits par lots
</span>
</th>
<th scope="col" class="px-6 py-3 w-48">
<span
class="text-center text-xs font-semibold uppercase tracking-wide text-gray-800 dark:text-gray-200"
>
Prix d'un lot HT
</span>
</th>
<th scope="col" class="px-6 py-3">
<span
class="text-center text-xs font-semibold uppercase tracking-wide text-gray-800 dark:text-gray-200"
>
TVA
</span>
</th>
<th scope="col" class="px-6 py-3 w-48">
<span
class="text-center text-xs font-semibold uppercase tracking-wide text-gray-800 dark:text-gray-200"
>
Prix d'un lot TTC
</span>
</th>
</tr>
</thead>
{#each selectedRestock.items as item}
<tr>
<td class="px-12 py-3">
<div class="flex flex-col">
<div
class="rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-gray-300 text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent"
>
<p>{item.item_name}</p>
</div>
</div>
</td>
<td class="px-3 py-3">
<div class="flex flex-col">
<div
class="rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-gray-300 text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent"
>
<p>{formatPrice(item.bundle_cost_ht * item.amount_of_bundle)}</p>
</div>
</div>
</td>
<td class="px-3 py-3">
<div class="flex flex-col">
<div
class="rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-gray-300 text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent"
>
<p>
{formatPrice(item.bundle_cost_ttc * item.amount_of_bundle)}
</p>
</div>
</div>
</td>
<td class="px-3 py-3">
<div class="flex flex-col">
<div
class="rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-gray-300 text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent"
>
<p>{item.amount_of_bundle}</p>
</div>
</div>
</td>
<td class="px-3 py-3">
<div class="flex flex-col">
<div
class="rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-gray-300 text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent"
>
<p>{item.amount_per_bundle}</p>
</div>
</div>
</td>
<td class="px-6 py-3">
<div class="flex flex-col">
<div
class="rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-gray-300 text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent"
>
<p>{formatPrice(item.bundle_cost_ht)}</p>
</div>
</div>
</td>
<td class="px-6 py-3">
<div class="flex flex-col">
<div
class="rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-gray-300 text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent"
>
<p>{item.tva / 100}%</p>
</div>
</div>
</td>
<td class="px-6 py-3">
<div class="flex flex-col">
<div
class="rounded-lg border-transparent flex-1 appearance-none border border-gray-300 w-full py-2 px-4 bg-gray-300 text-gray-700 placeholder-gray-400 shadow-sm text-base focus:outline-none focus:ring-2 focus:ring-blue-600 focus:border-transparent"
>
<p>{formatPrice(item.bundle_cost_ttc)}</p>
</div>
</div>
</td>
</tr>
{/each}
</table>
{/if}
</div>
Loading