Index: src/sections/invoice/invoice-new-edit-form.tsx
===================================================================
--- src/sections/invoice/invoice-new-edit-form.tsx	(revision 5d6f37a75130aab7d9a67b5b430603eff2c0ca6e)
+++ src/sections/invoice/invoice-new-edit-form.tsx	(revision 057453cdc828f504e8bcd813422d99221b3b5732)
@@ -11,5 +11,5 @@
 import { useRouter } from 'src/routes/hooks';
 // types
-import { Invoice } from 'mvpmasters-shared';
+import { CreateInvoice, Invoice } from 'src/schemas';
 // hooks
 import { useBoolean } from 'src/hooks/use-boolean';
@@ -20,19 +20,13 @@
 import uploadToFirebaseStorage from 'src/utils/upload-to-firebase-storage';
 import { pdf } from '@react-pdf/renderer';
-import { useGetSettings } from 'src/api/settings';
-import {
-  collections,
-  documents,
-  firestoreBatch,
-  generateId,
-  updateDocument,
-} from 'src/lib/firestore';
+import { useGetTenant } from 'src/api/tenant';
+import { collections, generateId, updateDocument } from 'src/lib/firestore';
 import { useGetServices } from 'src/api/service';
 import { mutate } from 'swr';
-import { Timestamp } from 'firebase/firestore';
 import InvoiceNewEditStatusDate from './invoice-new-edit-status-date';
 import InvoiceNewEditAddress from './invoice-new-edit-address';
 import InvoiceNewEditDetails from './invoice-new-edit-details';
 import InvoicePDF from './invoice-pdf';
+import { createInvoice, updateInvoice } from 'src/api/invoice';
 
 // ----------------------------------------------------------------------
@@ -58,4 +52,13 @@
 ];
 
+interface InvoiceItem {
+  service: string | null;
+  title: string;
+  price: number;
+  total: number;
+  quantity: number;
+  description: string;
+}
+
 export default function InvoiceNewEditForm({ isCopy, currentInvoice }: Props) {
   const router = useRouter();
@@ -69,5 +72,7 @@
     description: Yup.string().required('Description is required'),
     service: Yup.string().nullable(),
-    quantity: Yup.number().required('Quantity is required').min(0.5, 'Quantity must be at least 1'),
+    quantity: Yup.number()
+      .required('Quantity is required')
+      .min(0.5, 'Quantity must be at least 0.5'),
     price: Yup.number().required('Price is required').min(0, 'Price must be at least 0'),
     total: Yup.number().required('Total is required').min(0, 'Total must be at least 0'),
@@ -91,5 +96,5 @@
       .required('Quantity type is required'),
     month: Yup.string().oneOf(monthNames).required('Month is required'),
-    status: Yup.string().required(),
+    status: Yup.string().oneOf(['draft', 'processing', 'pending', 'overdue', 'paid']).required(),
     totalAmount: Yup.number().required(),
     // not required
@@ -103,5 +108,6 @@
 
   const { services: invoiceServices } = useGetServices();
-  const { settings, settingsEmpty, settingsLoading } = useGetSettings();
+  console.log('invoiceServices', invoiceServices);
+  const { settings: tenant, settingsEmpty, settingsLoading } = useGetTenant();
 
   const defaultValues = useMemo(
@@ -110,7 +116,9 @@
         !isCopy && currentInvoice?.invoiceNumber
           ? currentInvoice?.invoiceNumber
-          : incrementInvoiceNumber(settings?.invoice?.lastInvoiceNumber),
-      createDate: currentInvoice?.createDate?.toDate() || new Date(),
-      dueDate: currentInvoice?.dueDate?.toDate() || null,
+          : incrementInvoiceNumber(tenant?.lastInvoiceNumber),
+      createDate: currentInvoice?.createDate ? new Date(currentInvoice.createDate) : new Date(),
+      dueDate: currentInvoice?.dueDate
+        ? new Date(currentInvoice.dueDate)
+        : new Date(Date.now() + 30 * 24 * 60 * 60 * 1000),
       invoiceFrom: currentInvoice?.invoiceFrom || null,
       invoiceTo: currentInvoice?.invoiceTo || null,
@@ -138,5 +146,5 @@
       totalAmount: currentInvoice?.totalAmount || 0,
     }),
-    [currentInvoice, isCopy, settings]
+    [currentInvoice, isCopy, tenant]
   );
 
@@ -164,6 +172,17 @@
 
     try {
-      // generate collection id
       const id = generateId(collections.invoice);
+
+      // Ensure dates are valid Date objects
+      const createDate =
+        data.createDate instanceof Date ? data.createDate : new Date(data.createDate);
+      const dueDate = data.dueDate instanceof Date ? data.dueDate : new Date(data.dueDate);
+
+      const currentTime = new Date();
+      createDate.setHours(
+        currentTime.getHours(),
+        currentTime.getMinutes(),
+        currentTime.getSeconds()
+      );
 
       // attach serivce details
@@ -173,21 +192,16 @@
       }));
 
-      const currentTime = new Date();
-      const createDateWithCurrentTime = new Date(data.createDate); // This creates a date object using the date from data.createDate
-      // Set the time of createDateWithCurrentTime to the current hour, minutes, and seconds
-      createDateWithCurrentTime.setHours(
-        currentTime.getHours(),
-        currentTime.getMinutes(),
-        currentTime.getSeconds()
-      );
-
       // transform data
-      const writeData = {
+      const writeData: CreateInvoice = {
         ...data,
-        invoiceNumber: incrementInvoiceNumber(settings?.invoice.lastInvoiceNumber),
+        invoiceNumber: incrementInvoiceNumber(tenant?.lastInvoiceNumber),
         status: 'draft',
-        createDate: Timestamp.fromDate(createDateWithCurrentTime),
-        dueDate: Timestamp.fromDate(new Date(data.dueDate)),
-        items,
+        createDate,
+        dueDate,
+        items: items.filter((item) => item.service !== null) as CreateInvoice['items'],
+        subTotal: data.totalAmount - (data.taxes || 0) - (data.discount || 0),
+        month: data.month as Invoice['month'],
+        invoiceFrom: data.invoiceFrom!,
+        invoiceTo: data.invoiceTo!,
       };
 
@@ -199,19 +213,21 @@
 
       // write to DB
-      await firestoreBatch([
-        {
-          docPath: `${collections.invoice}/${id}`,
-          type: 'set',
-          data: {
-            ...writeData,
-            pdfRef: storagePath,
-          },
-        },
-        {
-          docPath: `${collections.settings}/${documents.settingsInvoice}`,
-          type: 'set',
-          data: { lastInvoiceNumber: writeData.invoiceNumber },
-        },
-      ]);
+      // await firestoreBatch([
+      //   {
+      //     docPath: `${collections.invoice}/${id}`,
+      //     type: 'set',
+      //     data: {
+      //       ...writeData,
+      //       pdfRef: storagePath,
+      //     },
+      //   },
+      //   {
+      //     docPath: `${collections.settings}/${documents.settingsInvoice}`,
+      //     type: 'set',
+      //     data: { lastInvoiceNumber: writeData.invoiceNumber },
+      //   },
+      // ]);
+
+      await createInvoice({ ...writeData, pdfRef: storagePath });
 
       loadingSave.onFalse();
@@ -229,4 +245,9 @@
     try {
       if (currentInvoice) {
+        // Ensure dates are valid Date objects
+        const createDate =
+          data.createDate instanceof Date ? data.createDate : new Date(data.createDate);
+        const dueDate = data.dueDate instanceof Date ? data.dueDate : new Date(data.dueDate);
+
         // attach serivce details
         const items = data.items.map((item) => ({
@@ -238,7 +259,9 @@
         const writeData = {
           ...data,
-          createDate: Timestamp.fromDate(new Date(data.createDate)),
-          dueDate: Timestamp.fromDate(new Date(data.dueDate)),
+          createDate,
+          dueDate,
           items,
+          invoiceFrom: data.invoiceFrom!,
+          invoiceTo: data.invoiceTo!,
         };
 
@@ -250,14 +273,46 @@
 
         // update DB
-        await updateDocument(collections.invoice, currentInvoice.id, {
+        // await updateDocument(collections.invoice, currentInvoice.id, {
+        //   ...writeData,
+        //   pdfRef: storagePath,
+        // });
+
+        await updateInvoice(currentInvoice.id, {
           ...writeData,
           pdfRef: storagePath,
+          status: data.status as 'draft' | 'processing' | 'pending' | 'overdue' | 'paid',
+          month: data.month as
+            | 'January'
+            | 'February'
+            | 'March'
+            | 'April'
+            | 'May'
+            | 'June'
+            | 'July'
+            | 'August'
+            | 'September'
+            | 'October'
+            | 'November'
+            | 'December',
+          items: items.filter((item) => item.service !== null) as {
+            service: { id: string; month: number; name: string; sprint: number; hour: number };
+            title: string;
+            price: number;
+            total: number;
+            quantity: number;
+            description: string;
+          }[],
         });
 
         // mutate current data
-        mutate([collections.invoice, currentInvoice.id]);
+        // mutate([collections.invoice, currentInvoice.id]);
       } else {
         // generate collection id
         const id = generateId(collections.invoice);
+
+        // Ensure dates are valid Date objects
+        const createDate =
+          data.createDate instanceof Date ? data.createDate : new Date(data.createDate);
+        const dueDate = data.dueDate instanceof Date ? data.dueDate : new Date(data.dueDate);
 
         // attach serivce details
@@ -270,7 +325,9 @@
         const writeData = {
           ...data,
-          createDate: Timestamp.fromDate(new Date(data.createDate)),
-          dueDate: Timestamp.fromDate(new Date(data.dueDate)),
+          createDate,
+          dueDate,
           items,
+          invoiceFrom: data.invoiceFrom!,
+          invoiceTo: data.invoiceTo!,
         };
 
@@ -282,19 +339,20 @@
 
         // write to DB
-        await firestoreBatch([
-          {
-            docPath: `${collections.invoice}/${id}`,
-            type: 'set',
-            data: {
-              ...writeData,
-              pdfRef: storagePath,
-            },
-          },
-          {
-            docPath: `${collections.settings}/${documents.settingsInvoice}`,
-            type: 'set',
-            data: { lastInvoiceNumber: writeData.invoiceNumber },
-          },
-        ]);
+        // await firestoreBatch([
+        //   {
+        //     docPath: `${collections.invoice}/${id}`,
+        //     type: 'set',
+        //     data: {
+        //       ...writeData,
+        //       pdfRef: storagePath,
+        //     },
+        //   },
+        //   {
+        //     docPath: `${collections.settings}/${documents.settingsInvoice}`,
+        //     type: 'set',
+        //     data: { lastInvoiceNumber: writeData.invoiceNumber },
+        //   },
+        // ]);
+        await createInvoice({ ...writeData, pdfRef: storagePath });
 
         reset();
@@ -314,5 +372,5 @@
     <FormProvider methods={methods}>
       <Card>
-        {settings?.company && <InvoiceNewEditAddress />}
+        {!!tenant && <InvoiceNewEditAddress />}
 
         <InvoiceNewEditStatusDate isCopy={isCopy} />
