feat: Assign default PDF template automatically

This commit is contained in:
Ahmed Bouhuolia
2024-10-02 18:18:57 +02:00
parent cbc60b3c73
commit b23112bc92
30 changed files with 501 additions and 46 deletions

View File

@@ -61,6 +61,7 @@ function InvoiceForm({
createInvoiceMutate,
editInvoiceMutate,
submitPayload,
saleInvoiceState
} = useInvoiceFormContext();
// Invoice number.
@@ -83,6 +84,7 @@ function InvoiceForm({
currency_code: base_currency,
invoice_message: defaultTo(invoiceCustomerNotes, ''),
terms_conditions: defaultTo(invoiceTermsConditions, ''),
pdf_template_id: saleInvoiceState?.defaultTemplateId,
...newInvoice,
}),
};

View File

@@ -16,13 +16,22 @@ import {
useEditInvoice,
useSettingsInvoices,
useEstimate,
useGetSaleInvoiceState,
GetSaleInvoiceStateResponse,
} from '@/hooks/query';
import { useProjects } from '@/containers/Projects/hooks';
import { useTaxRates } from '@/hooks/query/taxRates';
import { useGetPdfTemplates } from '@/hooks/query/pdf-templates';
import { useGetPaymentServices } from '@/hooks/query/payment-services';
const InvoiceFormContext = createContext();
interface InvoiceFormContextValue {
saleInvoiceState: GetSaleInvoiceStateResponse | null;
isInvoiceStateLoading: boolean;
}
const InvoiceFormContext = createContext<InvoiceFormContextValue>(
{} as InvoiceFormContextValue,
);
/**
* Accounts chart data provider.
@@ -100,6 +109,9 @@ function InvoiceFormProvider({ invoiceId, baseCurrency, ...props }) {
isSuccess: isBranchesSuccess,
} = useBranches({}, { enabled: isBranchFeatureCan });
const { data: saleInvoiceState, isLoading: isInvoiceStateLoading } =
useGetSaleInvoiceState();
// Handle fetching settings.
const { isLoading: isSettingsLoading } = useSettingsInvoices();
@@ -154,6 +166,10 @@ function InvoiceFormProvider({ invoiceId, baseCurrency, ...props }) {
// Payment Services
paymentServices,
isPaymentServicesLoading,
// Invoice state
saleInvoiceState,
isInvoiceStateLoading,
};
return (
@@ -172,6 +188,7 @@ function InvoiceFormProvider({ invoiceId, baseCurrency, ...props }) {
);
}
const useInvoiceFormContext = () => React.useContext(InvoiceFormContext);
const useInvoiceFormContext = () =>
React.useContext<InvoiceFormContextValue>(InvoiceFormContext);
export { InvoiceFormProvider, useInvoiceFormContext };