@erpsquad/common 1.8.137 → 1.8.138

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,7 +7,7 @@ import { useMemo, useState, useEffect, useCallback } from "react";
7
7
  import { useTranslation } from "react-i18next";
8
8
  import { l as loadModuleTranslations } from "../translations-BsYOCXBi.esm.js";
9
9
  import { m as modules } from "../modules-DSAqlzDP.esm.js";
10
- import { ay as checkSkuAndItemName, e as enqueueSnackbar } from "../common-hdEA-aLz.esm.js";
10
+ import { ay as checkSkuAndItemName, e as enqueueSnackbar, a7 as getToken } from "../common-hdEA-aLz.esm.js";
11
11
  import { u as useERPUI } from "../ERPUIProvider-yX9tP-eC.esm.js";
12
12
  import { b as b2, a as a2 } from "../ERPUIProvider-yX9tP-eC.esm.js";
13
13
  import { u as u2 } from "../useLangauge-DIZzO3H7.esm.js";
@@ -678,6 +678,159 @@ function useApi() {
678
678
  }, []);
679
679
  return { callApi, loading, error };
680
680
  }
681
+ let baseUrl = "";
682
+ let defaultHeaders = {};
683
+ const _getV1SettingsConfigurations = async (url, request) => {
684
+ const queryParameters = ["skip", "limit", "order", "filters", "search", "select"];
685
+ const searchParams = new URLSearchParams();
686
+ queryParameters.forEach((qp) => {
687
+ var _a;
688
+ if (request[qp]) {
689
+ if (Array.isArray(request[qp])) {
690
+ request[qp].forEach((p) => {
691
+ searchParams.append(qp, p);
692
+ });
693
+ } else {
694
+ searchParams.append(qp, ((_a = request[qp]) == null ? void 0 : _a.toString()) || "");
695
+ }
696
+ }
697
+ delete request[qp];
698
+ });
699
+ const headers = {
700
+ ...defaultHeaders
701
+ };
702
+ if (request["x-token"] !== void 0) {
703
+ headers["x-token"] = request["x-token"];
704
+ delete request["x-token"];
705
+ }
706
+ if (request["x-timezone"] !== void 0) {
707
+ headers["x-timezone"] = request["x-timezone"];
708
+ delete request["x-timezone"];
709
+ }
710
+ const response = await fetch(`${url}/v1/settings/configurations?${searchParams.toString()}`, {
711
+ headers
712
+ });
713
+ if (!response.ok) {
714
+ throw new Error(await response.text());
715
+ }
716
+ return await response.json();
717
+ };
718
+ const getV1SettingsConfigurations = async (request) => {
719
+ return await _getV1SettingsConfigurations(baseUrl, request);
720
+ };
721
+ const useFormFieldUpdater = ({
722
+ control,
723
+ setValue,
724
+ fieldToUpdate,
725
+ transformResponse,
726
+ watch,
727
+ fieldArrayName,
728
+ companyField = "company_id",
729
+ locationField = "location_id",
730
+ departmentField = "department_id",
731
+ customDependencies = []
732
+ }) => {
733
+ const { callApi } = useApi();
734
+ const [initialState, setInitialState] = useState(null);
735
+ const companyPath = fieldArrayName ? `${fieldArrayName}.${companyField}` : companyField;
736
+ const locationPath = fieldArrayName ? `${fieldArrayName}.${locationField}` : locationField;
737
+ const departmentPath = fieldArrayName ? `${fieldArrayName}.${departmentField}` : departmentField;
738
+ const data = (watch == null ? void 0 : watch(`${fieldArrayName}`)) || {};
739
+ const selectedCompany = (data == null ? void 0 : data[companyField]) || null;
740
+ const selectedLocation = (data == null ? void 0 : data[locationField]) || null;
741
+ const selectedDepartment = (data == null ? void 0 : data[departmentField]) || null;
742
+ console.log(selectedCompany, selectedLocation, selectedDepartment);
743
+ useEffect(() => {
744
+ if (watch && !initialState) {
745
+ const filters = {
746
+ [companyField]: selectedCompany,
747
+ [locationField]: selectedLocation,
748
+ [departmentField]: selectedDepartment
749
+ };
750
+ const values = {};
751
+ Object.keys(fieldToUpdate).forEach((key) => {
752
+ const path = fieldArrayName ? `${fieldArrayName}.${key}` : key;
753
+ values[key] = watch(path);
754
+ });
755
+ setInitialState({ filters, values });
756
+ }
757
+ }, [watch, selectedCompany, selectedLocation, selectedDepartment, initialState, companyPath, locationPath, departmentPath]);
758
+ const updateFields = useCallback((data2) => {
759
+ if (!setValue || !data2) return;
760
+ const transformed = transformResponse ? transformResponse(data2) : data2;
761
+ if (!transformed) return;
762
+ Object.entries(fieldToUpdate).forEach(([sourceField, targetField]) => {
763
+ const updateValue = transformed == null ? void 0 : transformed.find((i) => (i == null ? void 0 : i.config_key) == (typeof targetField == "object" ? targetField.key : targetField));
764
+ console.log("updateValue", updateValue);
765
+ if (updateValue !== void 0 && updateValue !== null) {
766
+ if (typeof targetField == "object") {
767
+ targetField.value((updateValue == null ? void 0 : updateValue.reference_id) || (updateValue == null ? void 0 : updateValue.reference_text));
768
+ } else {
769
+ const path = fieldArrayName ? `${fieldArrayName}.${sourceField}` : sourceField;
770
+ setValue(path, (updateValue == null ? void 0 : updateValue.reference_id) || (updateValue == null ? void 0 : updateValue.reference_text));
771
+ }
772
+ }
773
+ });
774
+ }, [setValue, fieldToUpdate, transformResponse, fieldArrayName, ...customDependencies]);
775
+ const fetchAndUpdate = useCallback(async () => {
776
+ var _a, _b, _c;
777
+ if (!selectedCompany || !initialState) return;
778
+ const isFilterChanged = JSON.stringify((_a = initialState == null ? void 0 : initialState.filters) == null ? void 0 : _a[companyField]) !== JSON.stringify(selectedCompany) || JSON.stringify((_b = initialState == null ? void 0 : initialState.filters) == null ? void 0 : _b[locationField]) !== JSON.stringify(selectedLocation) || JSON.stringify((_c = initialState == null ? void 0 : initialState.filters) == null ? void 0 : _c[departmentField]) !== JSON.stringify(selectedDepartment);
779
+ console.log("isFilterChanged", isFilterChanged);
780
+ if (!isFilterChanged) {
781
+ Object.entries(initialState.values).forEach(([key, value]) => {
782
+ const path = fieldArrayName ? `${fieldArrayName}.${key}` : key;
783
+ setValue == null ? void 0 : setValue(path, value);
784
+ });
785
+ return;
786
+ }
787
+ console.log("isFilterChanged", isFilterChanged);
788
+ const filter = `(company_id.eq=${selectedCompany || 1}&location_id.eq=${selectedLocation || null}&department_id.eq=${selectedDepartment || null})`;
789
+ callApi({
790
+ apiFunc: () => getV1SettingsConfigurations({
791
+ ...getToken(),
792
+ filters: filter
793
+ }),
794
+ onSuccess: (res) => {
795
+ var _a2;
796
+ updateFields((_a2 = res == null ? void 0 : res.data) == null ? void 0 : _a2.configuration_list);
797
+ },
798
+ onError: (err) => {
799
+ console.error("Error fetching accounting settings:", err);
800
+ }
801
+ });
802
+ }, [selectedCompany, selectedLocation, selectedDepartment, initialState, callApi, setValue]);
803
+ useEffect(() => {
804
+ fetchAndUpdate();
805
+ }, [fetchAndUpdate]);
806
+ return {
807
+ selectedCompany,
808
+ selectedLocation,
809
+ selectedDepartment
810
+ };
811
+ };
812
+ const useAccountingSetting = ({
813
+ fieldToUpdate,
814
+ fieldArrayName,
815
+ companyField = "company_id",
816
+ locationField = "location_id",
817
+ departmentField = "department_id",
818
+ formMethods,
819
+ customDependencies = []
820
+ }) => {
821
+ const { setValue, control, watch } = formMethods || {};
822
+ return useFormFieldUpdater({
823
+ control,
824
+ setValue,
825
+ watch,
826
+ companyField,
827
+ locationField,
828
+ departmentField,
829
+ fieldToUpdate,
830
+ fieldArrayName,
831
+ customDependencies
832
+ });
833
+ };
681
834
  export {
682
835
  b2 as apiHelper,
683
836
  apiType,
@@ -692,11 +845,13 @@ export {
692
845
  recursiveSort,
693
846
  removeExtraKeys,
694
847
  sortData,
848
+ useAccountingSetting,
695
849
  useApi,
696
850
  useAppTranslations,
697
851
  useAuth,
698
852
  a2 as useDataFetcher,
699
853
  u as useDeepMemo,
854
+ useFormFieldUpdater,
700
855
  u2 as useLanguage,
701
856
  useLanguageFallback,
702
857
  useLanguageState,