import odooApi from '@/core/api/odooApi'; import HookFormSelect from '@/core/components/elements/Select/HookFormSelect'; import useAuth from '@/core/hooks/useAuth'; import addressApi from '@/lib/address/api/addressApi'; import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline'; import { useEffect, useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; import { toast } from 'react-hot-toast'; import BottomPopup from '@/core/components/elements/Popup/BottomPopup'; import { yupResolver } from '@hookform/resolvers/yup'; import * as Yup from 'yup'; import SwitchAccount from '@/lib/auth/components/SwitchAccount'; import { Checkbox } from '@chakra-ui/react'; const CompanyProfile = () => { const [changeConfirmation, setChangeConfirmation] = useState(false); const [changeType, setChangeType] = useState(false); const [isChecked, setIsChecked] = useState(false); const [company_type, setCompany_type] = useState('nonpkp'); const auth = useAuth(); const [isOpen, setIsOpen] = useState(false); const toggle = () => setIsOpen(!isOpen); const { register, formState: { errors }, setValue, control, handleSubmit, } = useForm({ resolver: yupResolver(validationSchema), defaultValues, }); const [industries, setIndustries] = useState([]); useEffect(() => { const loadIndustries = async () => { const dataIndustries = await odooApi('GET', '/api/v1/partner/industry'); setIndustries( dataIndustries?.map((o) => ({ value: o.id, label: o.name })) ); }; loadIndustries(); }, []); const [companyTypes, setCompanyTypes] = useState([]); useEffect(() => { const loadCompanyTypes = async () => { const dataCompanyTypes = await odooApi( 'GET', '/api/v1/partner/company_type' ); setCompanyTypes( dataCompanyTypes?.map((o) => ({ value: o.id, label: o.name })) ); }; loadCompanyTypes(); }, []); useEffect(() => { const loadProfile = async () => { const dataProfile = await addressApi({ id: auth?.company ? auth.parentId ? auth.parentId : auth.partnerId : auth.partnerId, }); setCompany_type(dataProfile?.companyType); setValue('name', dataProfile?.name); setValue('industry', dataProfile?.industryId); setValue('companyType', dataProfile?.companyTypeId); setValue('taxName', dataProfile?.taxName); setValue('npwp', dataProfile?.npwp); setValue('alamat_wajib_pajak', dataProfile?.alamatWajibPajak); setValue('alamat_bisnis', dataProfile?.alamatBisnis); setValue('company_type', dataProfile?.companyType); setValue('email_bisnis', dataProfile.email); setValue('mobile_bisnis', dataProfile.mobile); }; if (auth) loadProfile(); }, [auth, setValue]); const onSubmitHandler = async (values) => { if (changeConfirmation) { const data = { ...values, id_user: auth.partnerId, company_type_id: values.companyType, industry_id: values.industry, tax_name: values.taxName, alamat_lengkap_text: values.alamat_wajib_pajak, street: values.alamat_bisnis, email: values.email_bisnis, mobile: values.mobile_bisnis, }; const isUpdated = await odooApi( 'PUT', `/api/v1/partner/${auth.parentId}`, data ); if (isUpdated?.id) { toast.success('Berhasil mengubah profil', { duration: 1500 }); return; } toast.error('Terjadi kesalahan internal'); } }; const handleConfirmSubmit = () => { setChangeConfirmation(false); handleSubmit(onSubmitHandler)(); }; const handleConfirmSubmitType = () => { setChangeType(false); setIsChecked(true); setIsOpen(!isOpen); }; const handleChange = async () => { if (isChecked) { setIsChecked(!isChecked); setIsOpen(!isOpen); } else { setIsChecked(!isChecked); setChangeType(true); } }; return ( <> setChangeType(false)} // Menutup popup title='Ubah type akun' >
Anda akan mengubah type akun anda?
setChangeConfirmation(true)} title='Ubah profil Bisnis' >
Apakah anda yakin mengubah data bisnis?
{company_type === 'nonpkp' && (

Ubah ke akun PKP

)}

Informasi Usaha

{company_type.toUpperCase()}
Dibawah ini adalah data usaha yang anda masukkan, periksa kembali data usaha anda.
{/* */}
{!isOpen && (
{ e.preventDefault(); setChangeConfirmation(true); }} >
( )} />
{errors.industry?.message}
Badan Usaha
( )} />
{errors.companyType?.message}
{errors.name?.message}
{errors.taxName?.message}
{errors.email_bisnis?.message}
{errors.mobile_bisnis?.message}
{errors.alamat_wajib_pajak?.message}
{errors.alamat_bisnis?.message}
*Untuk mengganti NPWP bisa menghubungi kami{' '} disini.
{errors.npwp?.message}
)} {isOpen && } ); }; export default CompanyProfile; const validationSchema = Yup.object().shape({ alamat_bisnis: Yup.string().required('Harus di-isi'), name: Yup.string().required('Harus di-isi'), email_bisnis: Yup.string().required('Harus di-isi'), mobile_bisnis: Yup.string().required('Harus di-isi'), industry: Yup.string().required('Harus di-pilih'), companyType: Yup.string().required('Harus di-pilih'), taxName: Yup.string(), npwp: Yup.string(), alamat_wajib_pajak: Yup.string(), company_type: Yup.string(), taxName: Yup.string().when('company_type', { is: (company_type) => company_type !== 'Non PKP', then: Yup.string().required('Harus di-isi'), otherwise: Yup.string().notRequired(), }), npwp: Yup.string().when('company_type', { is: (company_type) => company_type !== 'Non PKP', then: Yup.string().required('Harus di-isi'), otherwise: Yup.string().notRequired(), }), alamat_wajib_pajak: Yup.string().when('company_type', { is: (company_type) => company_type !== 'Non PKP', then: Yup.string().required('Harus di-isi'), otherwise: Yup.string().notRequired(), }), }); const defaultValues = { industry: '', companyType: '', name: '', email_bisnis: '', mobile_bisnis: '', taxName: '', npwp: '', alamat_wajib_pajak: '', alamat_bisnis: '', };