diff --git a/src/types/services/vendor.ts b/src/types/services/vendor.ts index 367a7d1..2d098ae 100644 --- a/src/types/services/vendor.ts +++ b/src/types/services/vendor.ts @@ -21,3 +21,15 @@ export interface Vendors { limit: number total_pages: number } + +export interface VendorRequest { + name: string + email?: string + phone_number?: string + address?: string + contact_person?: string + tax_number?: string + payment_terms?: string + notes?: string + is_active: boolean +} diff --git a/src/views/apps/vendor/list/AddVendorDrawer.tsx b/src/views/apps/vendor/list/AddVendorDrawer.tsx index 42a1c3d..d84704b 100644 --- a/src/views/apps/vendor/list/AddVendorDrawer.tsx +++ b/src/views/apps/vendor/list/AddVendorDrawer.tsx @@ -10,54 +10,82 @@ import Typography from '@mui/material/Typography' import Divider from '@mui/material/Divider' import Grid from '@mui/material/Grid2' import Box from '@mui/material/Box' +import Switch from '@mui/material/Switch' +import FormControlLabel from '@mui/material/FormControlLabel' // Third-party Imports import { useForm, Controller } from 'react-hook-form' -// Types Imports -import type { VendorType } from '@/types/apps/vendorTypes' - // Component Imports import CustomTextField from '@core/components/mui/TextField' +// Backend Types +export interface VendorRequest { + name: string + email?: string + phone_number?: string + address?: string + contact_person?: string + tax_number?: string + payment_terms?: string + notes?: string + is_active: boolean +} + +export interface Vendor { + id: string + organization_id: string + name: string + email?: string + phone_number?: string + address?: string + contact_person?: string + tax_number?: string + payment_terms?: string + notes?: string + is_active: boolean + created_at: string + updated_at: string +} + type Props = { open: boolean handleClose: () => void - vendorData?: VendorType[] - setData: (data: VendorType[]) => void + onSubmit?: (vendorRequest: VendorRequest) => Promise } type FormValidateType = { name: string - company: string email: string - telephone: string + phone_number: string + address: string + contact_person: string + tax_number: string + payment_terms: string + notes: string + is_active: boolean } -// Vars -const initialData = { +// Initial form data +const initialData: FormValidateType = { name: '', - company: '', email: '', - telephone: '' + phone_number: '', + address: '', + contact_person: '', + tax_number: '', + payment_terms: '', + notes: '', + is_active: true } const AddVendorDrawer = (props: Props) => { // Props - const { open, handleClose, vendorData, setData } = props + const { open, handleClose, onSubmit } = props // States const [showMore, setShowMore] = useState(false) - const [alamatPengiriman, setAlamatPengiriman] = useState(['']) - const [rekeningBank, setRekeningBank] = useState([ - { - bank: '', - cabang: '', - namaPemilik: '', - nomorRekening: '' - } - ]) - const [showPemetaanAkun, setShowPemetaanAkun] = useState(false) + const [isSubmitting, setIsSubmitting] = useState(false) // Hooks const { @@ -69,92 +97,58 @@ const AddVendorDrawer = (props: Props) => { defaultValues: initialData }) - // Functions untuk alamat - const handleTambahAlamat = () => { - setAlamatPengiriman([...alamatPengiriman, '']) - } + const handleFormSubmit = async (data: FormValidateType) => { + try { + setIsSubmitting(true) - const handleHapusAlamat = (index: number) => { - if (alamatPengiriman.length > 1) { - const newAlamat = alamatPengiriman.filter((_, i) => i !== index) - setAlamatPengiriman(newAlamat) - } - } - - const handleChangeAlamat = (index: number, value: string) => { - const newAlamat = [...alamatPengiriman] - newAlamat[index] = value - setAlamatPengiriman(newAlamat) - } - - // Functions untuk rekening bank - const handleTambahRekening = () => { - setRekeningBank([ - ...rekeningBank, - { - bank: '', - cabang: '', - namaPemilik: '', - nomorRekening: '' + // Create VendorRequest object + const vendorRequest: VendorRequest = { + name: data.name, + email: data.email || undefined, + phone_number: data.phone_number || undefined, + address: data.address || undefined, + contact_person: data.contact_person || undefined, + tax_number: data.tax_number || undefined, + payment_terms: data.payment_terms || undefined, + notes: data.notes || undefined, + is_active: data.is_active } - ]) - } - const handleHapusRekening = (index: number) => { - if (rekeningBank.length > 1) { - const newRekening = rekeningBank.filter((_, i) => i !== index) - setRekeningBank(newRekening) - } - } - - const handleChangeRekening = (index: number, field: string, value: string) => { - const newRekening = [...rekeningBank] - newRekening[index] = { ...newRekening[index], [field]: value } - setRekeningBank(newRekening) - } - - const onSubmit = (data: FormValidateType) => { - const newVendor: VendorType = { - id: (vendorData?.length && vendorData?.length + 1) || 1, - photo: '', - name: data.name, - company: data.company, - email: data.email, - telephone: data.telephone, - youPayable: 0, - theyPayable: 0 - } - - setData([...(vendorData ?? []), newVendor]) - handleClose() - resetForm(initialData) - setAlamatPengiriman(['']) - setRekeningBank([ - { - bank: '', - cabang: '', - namaPemilik: '', - nomorRekening: '' + // Call the onSubmit prop if provided (for API call) + if (onSubmit) { + await onSubmit(vendorRequest) + } else { + // Fallback: Create local vendor object for local state update + const newVendor: Vendor = { + id: `temp-${Date.now()}`, // Temporary ID + organization_id: 'current-org', // Should be provided by context + name: vendorRequest.name, + email: vendorRequest.email, + phone_number: vendorRequest.phone_number, + address: vendorRequest.address, + contact_person: vendorRequest.contact_person, + tax_number: vendorRequest.tax_number, + payment_terms: vendorRequest.payment_terms, + notes: vendorRequest.notes, + is_active: vendorRequest.is_active, + created_at: new Date().toISOString(), + updated_at: new Date().toISOString() + } } - ]) - setShowMore(false) - setShowPemetaanAkun(false) + + handleReset() + } catch (error) { + console.error('Error submitting vendor:', error) + // Handle error (show toast, etc.) + } finally { + setIsSubmitting(false) + } } const handleReset = () => { handleClose() resetForm(initialData) - setAlamatPengiriman(['']) - setRekeningBank([ - { - bank: '', - cabang: '', - namaPemilik: '', - nomorRekening: '' - } - ]) setShowMore(false) - setShowPemetaanAkun(false) } return ( @@ -194,472 +188,182 @@ const AddVendorDrawer = (props: Props) => { {/* Scrollable Content */} -
onSubmit(data))}> +
- {/* Tampilkan Foto */} -
- - - Tampilkan Foto - -
- - {/* Nama */} + {/* Nama Vendor */}
- Nama * + Nama Vendor * - - - - Tuan - Nyonya - Nona - Bapak - Ibu - - - - ( - - )} + ( + - - + )} + />
- {/* Perusahaan dan Telepon */} - - - ( - - )} - /> - - - ( - - )} - /> - - - {/* Email */} - ( - - )} - /> +
+ + Email + + ( + + )} + /> +
+ + {/* Nomor Telepon */} +
+ + Nomor Telepon + + } + /> +
+ + {/* Contact Person */} +
+ + Contact Person + + } + /> +
+ + {/* Status Aktif */} +
+ ( + } + label='Vendor Aktif' + /> + )} + /> +
{/* Tampilkan selengkapnya */} {!showMore && ( -
setShowMore(true)}> +
setShowMore(true)}> - + Tampilkan selengkapnya
)} - {/* Konten tambahan yang muncul saat showMore true */} + {/* Konten tambahan */} {showMore && ( <> - {/* Alamat Penagihan */} + {/* Alamat */}
- Alamat Penagihan + Alamat - + ( + + )} + />
- {/* Negara */} -
- - Negara - - - Indonesia - -
- - {/* Provinsi dan Kota */} - - - - Provinsi - - - Pilih Provinsi - DKI Jakarta - Jawa Barat - Jawa Tengah - Jawa Timur - - - - - Kota - - - Pilih Kota - Jakarta - Bandung - Surabaya - - - - - {/* Kecamatan dan Kelurahan */} - - - - Kecamatan - - - Pilih Kecamatan - - - - - Kelurahan - - - Pilih Kelurahan - - - - - {/* Tipe Kartu Identitas dan ID */} - - - - Tipe Kartu Identitas - - - Pilih Tipe Kartu Identitas - KTP - SIM - Paspor - - - - - ID Kartu Identitas - - - - - - {/* NPWP */} + {/* NPWP/Tax Number */}
NPWP - + } + />
- {/* Alamat Pengiriman */} + {/* Payment Terms */}
- - Alamat Pengiriman + + Syarat Pembayaran - {alamatPengiriman.map((alamat, index) => ( -
+ ( + + Pilih Syarat Pembayaran + Cash + Net 7 Hari + Net 14 Hari + Net 30 Hari + Net 60 Hari + Net 90 Hari + + )} + /> +
+ + {/* Notes */} +
+ + Catatan + + ( handleChangeAlamat(index, e.target.value)} - sx={{ - '& .MuiOutlinedInput-root': { - borderColor: index === 1 ? 'primary.main' : 'default' - } - }} + rows={3} /> - {alamatPengiriman.length > 1 && ( - handleHapusAlamat(index)} - sx={{ - color: 'error.main', - border: 1, - borderColor: 'error.main', - '&:hover': { - backgroundColor: 'error.light', - borderColor: 'error.main' - } - }} - > - - - )} -
- ))} + )} + />
- {/* Tambah Alamat Pengiriman */} -
- - - Tambah Alamat Pengiriman + {/* Sembunyikan */} +
setShowMore(false)}> + + + Sembunyikan
- - {/* Rekening Bank */} -
- - Rekening Bank - - {rekeningBank.map((rekening, index) => ( -
-
-
- {/* Baris pertama: Bank & Cabang */} - - - handleChangeRekening(index, 'bank', e.target.value)} - > - Pilih Bank - BCA - Mandiri - BNI - BRI - - - - handleChangeRekening(index, 'cabang', e.target.value)} - /> - - - - {/* Baris kedua: Nama Pemilik & Nomor Rekening */} - - - handleChangeRekening(index, 'namaPemilik', e.target.value)} - /> - - - handleChangeRekening(index, 'nomorRekening', e.target.value)} - /> - - -
- - {/* Tombol hapus di samping, sejajar dengan tengah kedua baris */} - {rekeningBank.length > 1 && ( -
- handleHapusRekening(index)} - sx={{ - color: 'error.main', - border: 1, - borderColor: 'error.main', - '&:hover': { - backgroundColor: 'error.light', - borderColor: 'error.main' - } - }} - > - - -
- )} -
-
- ))} - -
- - - Tambah Rekening Bank - -
- -
setShowPemetaanAkun(!showPemetaanAkun)}> - - - {showPemetaanAkun ? 'Sembunyikan pemetaan akun' : 'Tampilkan pemetaan akun'} - -
- - {/* Konten Pemetaan Akun */} - {showPemetaanAkun && ( -
- {/* Akun Hutang */} - - - - Akun Hutang - - - 2-20100 Hutang Usaha - 2-20200 Hutang Bank - 2-20300 Hutang Lainnya - - - - - Maksimal Hutang - - - - - - - {/* Akun Piutang */} - - - - Akun Piutang - - - 1-10100 Piutang Usaha - 1-10200 Piutang Karyawan - 1-10300 Piutang Lainnya - - - - - Maksimal Piutang - - - - - - - {/* Kena Pajak */} -
- - Kena pajak ? - -
- - -
-
-
- )} - - - - - Nomor - - - - - - Tanggal Lahir - - - - - -
- - Deskripsi - - -
- - {/* Button Sembunyikan di dalam konten */} -
setShowMore(false)}> - - - Sembunyikan - -
-
)}
@@ -679,10 +383,10 @@ const AddVendorDrawer = (props: Props) => { }} >
- -
diff --git a/src/views/apps/vendor/list/VendorListTable.tsx b/src/views/apps/vendor/list/VendorListTable.tsx index 9cf4527..064fa4c 100644 --- a/src/views/apps/vendor/list/VendorListTable.tsx +++ b/src/views/apps/vendor/list/VendorListTable.tsx @@ -350,12 +350,7 @@ const VendorListTable = () => { disabled={isLoading} /> - {/* setAddVendorOpen(!addVendorOpen)} - vendorData={data} - setData={setData} - /> */} + setAddVendorOpen(!addVendorOpen)} /> ) }