288 lines
7.2 KiB
TypeScript
288 lines
7.2 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import { ref, computed } from "vue";
|
|
import SupplierInvoiceService from "@/services/supplierInvoice";
|
|
|
|
import type {
|
|
SupplierInvoice,
|
|
CreateSupplierInvoicePayload,
|
|
UpdateSupplierInvoicePayload,
|
|
} from "@/services/supplierInvoice";
|
|
|
|
export const useSupplierInvoiceStore = defineStore("supplierInvoice", () => {
|
|
const supplierInvoices = ref<SupplierInvoice[]>([]);
|
|
const currentSupplierInvoice = ref<SupplierInvoice | null>(null);
|
|
const loading = ref(false);
|
|
const error = ref<string | null>(null);
|
|
|
|
const pagination = ref({
|
|
current_page: 1,
|
|
last_page: 1,
|
|
per_page: 10,
|
|
total: 0,
|
|
});
|
|
|
|
const allSupplierInvoices = computed(() => supplierInvoices.value);
|
|
const draftInvoices = computed(() =>
|
|
supplierInvoices.value.filter((invoice) => invoice.status === "brouillon")
|
|
);
|
|
const pendingInvoices = computed(() =>
|
|
supplierInvoices.value.filter((invoice) => invoice.status === "en_attente")
|
|
);
|
|
const paidInvoices = computed(() =>
|
|
supplierInvoices.value.filter((invoice) => invoice.status === "payee")
|
|
);
|
|
const cancelledInvoices = computed(() =>
|
|
supplierInvoices.value.filter((invoice) => invoice.status === "annulee")
|
|
);
|
|
const isLoading = computed(() => loading.value);
|
|
const hasError = computed(() => error.value !== null);
|
|
const getError = computed(() => error.value);
|
|
const getSupplierInvoiceById = computed(() => (id: number) =>
|
|
supplierInvoices.value.find((invoice) => invoice.id === id)
|
|
);
|
|
const getPagination = computed(() => pagination.value);
|
|
|
|
const setLoading = (isLoading: boolean) => {
|
|
loading.value = isLoading;
|
|
};
|
|
|
|
const setError = (err: string | null) => {
|
|
error.value = err;
|
|
};
|
|
|
|
const clearError = () => {
|
|
error.value = null;
|
|
};
|
|
|
|
const setSupplierInvoices = (newSupplierInvoices: SupplierInvoice[]) => {
|
|
supplierInvoices.value = newSupplierInvoices;
|
|
};
|
|
|
|
const setCurrentSupplierInvoice = (invoice: SupplierInvoice | null) => {
|
|
currentSupplierInvoice.value = invoice;
|
|
};
|
|
|
|
const setPagination = (meta: any) => {
|
|
if (meta) {
|
|
pagination.value = {
|
|
current_page: meta.current_page || 1,
|
|
last_page: meta.last_page || 1,
|
|
per_page: meta.per_page || 10,
|
|
total: meta.total || 0,
|
|
};
|
|
}
|
|
};
|
|
|
|
const fetchSupplierInvoices = async (params?: {
|
|
page?: number;
|
|
per_page?: number;
|
|
search?: string;
|
|
status?: string;
|
|
fournisseur_id?: number;
|
|
}) => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await SupplierInvoiceService.getAllSupplierInvoices(
|
|
params
|
|
);
|
|
setSupplierInvoices(response.data);
|
|
if (response.meta) {
|
|
setPagination(response.meta);
|
|
}
|
|
return response;
|
|
} catch (err: any) {
|
|
const errorMessage =
|
|
err.response?.data?.message ||
|
|
err.message ||
|
|
"Erreur lors de la récupération des factures fournisseurs";
|
|
setError(errorMessage);
|
|
throw err;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const fetchSupplierInvoice = async (id: number) => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await SupplierInvoiceService.getSupplierInvoice(id);
|
|
setCurrentSupplierInvoice(response.data);
|
|
return response.data;
|
|
} catch (err: any) {
|
|
const errorMessage =
|
|
err.response?.data?.message ||
|
|
err.message ||
|
|
"Erreur lors de la récupération de la facture fournisseur";
|
|
setError(errorMessage);
|
|
throw err;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const createSupplierInvoice = async (
|
|
payload: CreateSupplierInvoicePayload
|
|
) => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await SupplierInvoiceService.createSupplierInvoice(
|
|
payload
|
|
);
|
|
supplierInvoices.value.push(response.data);
|
|
setCurrentSupplierInvoice(response.data);
|
|
return response.data;
|
|
} catch (err: any) {
|
|
const errorMessage =
|
|
err.response?.data?.message ||
|
|
err.message ||
|
|
"Erreur lors de la création de la facture fournisseur";
|
|
setError(errorMessage);
|
|
throw err;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const updateSupplierInvoice = async (
|
|
payload: UpdateSupplierInvoicePayload
|
|
) => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await SupplierInvoiceService.updateSupplierInvoice(
|
|
payload
|
|
);
|
|
const updatedInvoice = response.data;
|
|
|
|
const index = supplierInvoices.value.findIndex(
|
|
(invoice) => invoice.id === updatedInvoice.id
|
|
);
|
|
if (index !== -1) {
|
|
supplierInvoices.value[index] = updatedInvoice;
|
|
}
|
|
|
|
if (
|
|
currentSupplierInvoice.value &&
|
|
currentSupplierInvoice.value.id === updatedInvoice.id
|
|
) {
|
|
setCurrentSupplierInvoice(updatedInvoice);
|
|
}
|
|
|
|
return updatedInvoice;
|
|
} catch (err: any) {
|
|
const errorMessage =
|
|
err.response?.data?.message ||
|
|
err.message ||
|
|
"Erreur lors de la mise à jour de la facture fournisseur";
|
|
setError(errorMessage);
|
|
throw err;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const deleteSupplierInvoice = async (id: number) => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await SupplierInvoiceService.deleteSupplierInvoice(id);
|
|
|
|
supplierInvoices.value = supplierInvoices.value.filter(
|
|
(invoice) => invoice.id !== id
|
|
);
|
|
|
|
if (
|
|
currentSupplierInvoice.value &&
|
|
currentSupplierInvoice.value.id === id
|
|
) {
|
|
setCurrentSupplierInvoice(null);
|
|
}
|
|
|
|
return response;
|
|
} catch (err: any) {
|
|
const errorMessage =
|
|
err.response?.data?.message ||
|
|
err.message ||
|
|
"Erreur lors de la suppression de la facture fournisseur";
|
|
setError(errorMessage);
|
|
throw err;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const fetchByFournisseur = async (fournisseurId: number) => {
|
|
setLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const response = await SupplierInvoiceService.getByFournisseur(
|
|
fournisseurId
|
|
);
|
|
setSupplierInvoices(response.data);
|
|
return response.data;
|
|
} catch (err: any) {
|
|
const errorMessage =
|
|
err.response?.data?.message ||
|
|
err.message ||
|
|
"Erreur lors de la récupération des factures du fournisseur";
|
|
setError(errorMessage);
|
|
throw err;
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const clearCurrentSupplierInvoice = () => {
|
|
setCurrentSupplierInvoice(null);
|
|
};
|
|
|
|
const clearStore = () => {
|
|
supplierInvoices.value = [];
|
|
currentSupplierInvoice.value = null;
|
|
error.value = null;
|
|
pagination.value = {
|
|
current_page: 1,
|
|
last_page: 1,
|
|
per_page: 10,
|
|
total: 0,
|
|
};
|
|
};
|
|
|
|
return {
|
|
supplierInvoices,
|
|
currentSupplierInvoice,
|
|
loading,
|
|
error,
|
|
|
|
allSupplierInvoices,
|
|
draftInvoices,
|
|
pendingInvoices,
|
|
paidInvoices,
|
|
cancelledInvoices,
|
|
isLoading,
|
|
hasError,
|
|
getError,
|
|
getSupplierInvoiceById,
|
|
getPagination,
|
|
|
|
fetchSupplierInvoices,
|
|
fetchSupplierInvoice,
|
|
createSupplierInvoice,
|
|
updateSupplierInvoice,
|
|
deleteSupplierInvoice,
|
|
fetchByFournisseur,
|
|
clearCurrentSupplierInvoice,
|
|
clearStore,
|
|
clearError,
|
|
};
|
|
});
|