npm-pkg-hook 1.12.7 → 1.12.8
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.
- package/.babelrc +0 -0
- package/.env +5 -0
- package/.github/pull_request_template.md +18 -0
- package/.github/workflows/pepeline.yaml +30 -0
- package/dist/index.js +47755 -10080
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +47071 -9610
- package/dist/index.mjs.map +1 -0
- package/eslint.config.mts +153 -0
- package/expire.json +1 -0
- package/jsconfig.json +28 -0
- package/package.json +24 -61
- package/script.txt +7 -0
- package/src/config/client/errors.ts +39 -0
- package/src/config/client/index.ts +1 -0
- package/src/config/content/en.json +5 -0
- package/src/config/content/es.json +5 -0
- package/src/config/content/index.ts +16 -0
- package/src/cookies/index.ts +3 -0
- package/src/hooks/addTenMinutes/index.ts +14 -0
- package/src/hooks/calculateLogLatHaversine/index.ts +41 -0
- package/src/hooks/completeSchedules/index.ts +22 -0
- package/src/hooks/convertToMilitaryTime/index.ts +18 -0
- package/src/hooks/generateStoreURL/index.ts +41 -0
- package/src/hooks/generateTemplate/index.ts +66 -0
- package/src/hooks/getCardType/index.ts +24 -0
- package/src/hooks/getCategoriesWithProduct/helpers/index.ts +7 -0
- package/src/hooks/getCategoriesWithProduct/index.ts +62 -0
- package/src/hooks/getGlobalSession/index.ts +69 -0
- package/src/hooks/getSession/index.ts +72 -0
- package/src/hooks/getTodayTimestamps/index.ts +70 -0
- package/src/hooks/getTotalHours/index.ts +76 -0
- package/src/hooks/handleLogin/index.ts +14 -0
- package/src/hooks/index.ts +153 -0
- package/src/hooks/isTokenExpired/index.ts +17 -0
- package/src/hooks/newMessageSubscription/index.ts +28 -0
- package/src/hooks/newStoreOrderSubscription/index.ts +26 -0
- package/src/hooks/statusOpenStores/helpers/index.ts +124 -0
- package/src/hooks/statusOpenStores/index.ts +221 -0
- package/src/hooks/updateExtProductFoodsOptional/index.ts +49 -0
- package/src/hooks/useAcumulateDate/index.ts +18 -0
- package/src/hooks/useAllStoresPendingToRegister/index.ts +37 -0
- package/src/hooks/useAmountInput/index.ts +127 -0
- package/src/hooks/useAnimationFrame/index.ts +45 -0
- package/src/hooks/useAnimationText/index.ts +31 -0
- package/src/hooks/useAsideCart/helpers/index.ts +22 -0
- package/src/hooks/useAsideCart/index.ts +217 -0
- package/src/hooks/useAsideCart/queries.ts +10 -0
- package/src/hooks/useBanner/index.ts +20 -0
- package/src/hooks/useCart/index.ts +2 -0
- package/src/hooks/useCart/queries.ts +174 -0
- package/src/hooks/useCart/useCart/helpers/index.ts +79 -0
- package/src/hooks/useCart/useCart/index.ts +424 -0
- package/src/hooks/useCart/useGetCart/index.ts +34 -0
- package/src/hooks/useCatWithProduct/index.ts +54 -0
- package/src/hooks/useCatWithProduct/queries.ts +200 -0
- package/src/hooks/useCatWithProduct/types/index.ts +104 -0
- package/src/hooks/useCatWithProductClient/index.ts +57 -0
- package/src/hooks/useCatWithProductClient/queries.ts +56 -0
- package/src/hooks/useCategoriesProduct/index.ts +14 -0
- package/src/hooks/useCategoriesProduct/queries.ts +16 -0
- package/src/hooks/useCategoryInStore/index.ts +167 -0
- package/src/hooks/useCategoryInStore/queries.ts +78 -0
- package/src/hooks/useCategoryStore/index.ts +8 -0
- package/src/hooks/useCategoryStore/queries.ts +16 -0
- package/src/hooks/useChartData/index.ts +171 -0
- package/src/hooks/useChartData/useChartData/index.ts +200 -0
- package/src/hooks/useChartData/useChartDataAllOrders/index.ts +94 -0
- package/src/hooks/useChatRoomSubscription/index.ts +28 -0
- package/src/hooks/useCheckbox/index.ts +115 -0
- package/src/hooks/useCities/index.ts +14 -0
- package/src/hooks/useCities/queries.ts +12 -0
- package/src/hooks/useClients/index.ts +97 -0
- package/src/hooks/useClients/queries.ts +215 -0
- package/src/hooks/useColorByLetters/helpers/alea.ts +73 -0
- package/src/hooks/useColorByLetters/helpers/colors.ts +45 -0
- package/src/hooks/useColorByLetters/helpers/index.ts +22 -0
- package/src/hooks/useColorByLetters/helpers/mersenne_twister.ts +118 -0
- package/src/hooks/useColorByLetters/index.ts +24 -0
- package/src/hooks/useConnection/index.ts +24 -0
- package/src/hooks/useCountries/index.ts +20 -0
- package/src/hooks/useCountries/queries.ts +12 -0
- package/src/hooks/useCreateDeliveryTime/index.ts +51 -0
- package/src/hooks/useCreateOrderStatusType/index.ts +134 -0
- package/src/hooks/useCreateProduct/helpers/index.ts +23 -0
- package/src/hooks/useCreateProduct/helpers/manageCacheDataCatProduct/index.ts +52 -0
- package/src/hooks/useCreateProduct/helpers/useEditImageProduct/index.ts +182 -0
- package/src/hooks/useCreateProduct/index.ts +337 -0
- package/src/hooks/useCreateStorePendingToRegister/index.ts +17 -0
- package/src/hooks/useCreateStorePendingToRegister/queries.ts +10 -0
- package/src/hooks/useDashboardComponents/index.ts +50 -0
- package/src/hooks/useDeleteExtraProductFoods/index.ts +13 -0
- package/src/hooks/useDeleteSubProductOptional/index.ts +32 -0
- package/src/hooks/useDeleteSubProductOptional/queries.ts +10 -0
- package/src/hooks/useDeliveryTime/index.ts +29 -0
- package/src/hooks/useDepartments/index.ts +14 -0
- package/src/hooks/useDepartments/queries.ts +13 -0
- package/src/hooks/useDessert/helpers/index.ts +51 -0
- package/src/hooks/useDessert/index.ts +529 -0
- package/src/hooks/useDessertWithPrice/helpers/index.ts +76 -0
- package/src/hooks/useDessertWithPrice/index.ts +381 -0
- package/src/hooks/useDessertWithPrice/queries.ts +18 -0
- package/src/hooks/useDevWS/index.ts +53 -0
- package/src/hooks/useDevices/index.ts +2 -0
- package/src/hooks/useDevices/queries.ts +28 -0
- package/src/hooks/useDevices/useGetDevices.ts +28 -0
- package/src/hooks/useDevices/useRegisterDevices.ts +75 -0
- package/src/hooks/useDownloadReports/helpers/downloadFileFromResponse.ts +21 -0
- package/src/hooks/useDownloadReports/index.ts +2 -0
- package/src/hooks/useDownloadReports/useDownloadReportByDay/index.ts +105 -0
- package/src/hooks/useDownloadReports/useGetReportByDateRange/index.ts +116 -0
- package/src/hooks/useDrag/index.ts +79 -0
- package/src/hooks/useDropzone/index.ts +94 -0
- package/src/hooks/useDynamicAuth/index.ts +14 -0
- package/src/hooks/useDynamicAuth/queries.ts +24 -0
- package/src/hooks/useEditCategory/index.ts +41 -0
- package/src/hooks/useEditOneExtProductFoodOptional/index.ts +28 -0
- package/src/hooks/useEditOneExtProductFoodOptional/queries.ts +16 -0
- package/src/hooks/useEditSubProductOptional/index.ts +12 -0
- package/src/hooks/useEditSubProductOptional/queries.ts +10 -0
- package/src/hooks/useEmployee/index.ts +18 -0
- package/src/hooks/useEmployee/queries.ts +85 -0
- package/src/hooks/useEmployee/useCreateEmployee.ts +90 -0
- package/src/hooks/useEvent/index.ts +34 -0
- package/src/hooks/useFavoriteStores/index.ts +31 -0
- package/src/hooks/useFavoriteStores/queries.ts +54 -0
- package/src/hooks/useFetchJson/index.ts +25 -0
- package/src/hooks/useFetchMoreInteractions/index.jsx +39 -0
- package/src/hooks/useFilterConfigs/index.ts +173 -0
- package/src/hooks/useFingerprintjs/index.ts +197 -0
- package/src/hooks/useFormTools/index.ts +143 -0
- package/src/hooks/useFormatDate/index.ts +91 -0
- package/src/hooks/useFormatNumberPhone/index.ts +23 -0
- package/src/hooks/useFullScreenMode/index.ts +61 -0
- package/src/hooks/useGenerateNumberArray/index.ts +17 -0
- package/src/hooks/useGetAllLocationUser/index.ts +13 -0
- package/src/hooks/useGetAllLocationUser/queries.ts +44 -0
- package/src/hooks/useGetAllSales/index.ts +26 -0
- package/src/hooks/useGetCookies/index.ts +43 -0
- package/src/hooks/useGetExtProductFoodsSubOptionalAll/index.ts +15 -0
- package/src/hooks/useGetFoodRecomended/index.ts +33 -0
- package/src/hooks/useGetFoodRecomended/queries.ts +47 -0
- package/src/hooks/useGetMessagesToRoom/index.ts +23 -0
- package/src/hooks/useGetMinPrice/index.ts +13 -0
- package/src/hooks/useGetMinPrice/queries.ts +7 -0
- package/src/hooks/useGetOneStoreRating/index.ts +41 -0
- package/src/hooks/useGetOneStoreRating/queries.ts +18 -0
- package/src/hooks/useGetSalesAmountToday/index.ts +31 -0
- package/src/hooks/useGetStoreCookie/index.ts +21 -0
- package/src/hooks/useGoogleLogin/index.ts +169 -0
- package/src/hooks/useGoogleLogin/loadScript.ts +15 -0
- package/src/hooks/useGoogleLogin/removeScript.ts +7 -0
- package/src/hooks/useHover/index.ts +32 -0
- package/src/hooks/useImageOptimization/index.ts +32 -0
- package/src/hooks/useImageUploaderProduct/helper/canvasUtils.ts +146 -0
- package/src/hooks/useImageUploaderProduct/helper/getOrientation.ts +54 -0
- package/src/hooks/useImageUploaderProduct/helper/index.ts +5 -0
- package/src/hooks/useImageUploaderProduct/index.ts +295 -0
- package/src/hooks/useImageWeight/index.ts +55 -0
- package/src/hooks/useImagesStore/index.ts +227 -0
- package/src/hooks/useImagesStore/queries.ts +193 -0
- package/src/hooks/useImagesStore/utils/index.ts +4 -0
- package/src/hooks/useIncomingOrders/index.ts +11 -0
- package/src/hooks/useIncomingOrders/queries.ts +87 -0
- package/src/hooks/useInnerHtml/index.ts +39 -0
- package/src/hooks/useIntersection/index.ts +84 -0
- package/src/hooks/useInventory/index.ts +2 -0
- package/src/hooks/useInventory/queries.ts +58 -0
- package/src/hooks/useInventory/useGetProductsInStock.ts +16 -0
- package/src/hooks/useInventory/useUpdateManageStock.ts +41 -0
- package/src/hooks/useKeypress/index.ts +28 -0
- package/src/hooks/useLazyScript/index.ts +74 -0
- package/src/hooks/useLocalBackendIp/index.ts +34 -0
- package/src/hooks/useLocalSorage/index.ts +36 -0
- package/src/hooks/useLocationManager/index.ts +63 -0
- package/src/hooks/useLocationNavigate/index.ts +54 -0
- package/src/hooks/useLoginEmployeeInStore/index.ts +38 -0
- package/src/hooks/useLogout/helpers/BroadcastChannel.ts +74 -0
- package/src/hooks/useLogout/helpers/apiBaseUrl.ts +12 -0
- package/src/hooks/useLogout/helpers/fetchData.ts +37 -0
- package/src/hooks/useLogout/helpers/getCsrfToken.ts +37 -0
- package/src/hooks/useLogout/helpers/index.ts +65 -0
- package/src/hooks/useLogout/helpers/logger.ts +88 -0
- package/src/hooks/useLogout/helpers/parseUrl.ts +39 -0
- package/src/hooks/useLogout/index.ts +105 -0
- package/src/hooks/useManageNewOrder/helpers/index.ts +45 -0
- package/src/hooks/useManageNewOrder/helpers/mock.ts +0 -0
- package/src/hooks/useManageNewOrder/index.ts +104 -0
- package/src/hooks/useManageQueryParams/index.ts +120 -0
- package/src/hooks/useMobile/index.ts +65 -0
- package/src/hooks/useModules/helpers/index.ts +1 -0
- package/src/hooks/useModules/helpers/validateModules.ts +29 -0
- package/src/hooks/useModules/index.ts +124 -0
- package/src/hooks/useMouse/index.ts +55 -0
- package/src/hooks/useMutateHeight/index.ts +36 -0
- package/src/hooks/useOrderClient/index.ts +5 -0
- package/src/hooks/useOrderStatusTypes/index.ts +2 -0
- package/src/hooks/useOrderStatusTypes/useOrderStatusTypes/index.ts +133 -0
- package/src/hooks/useOrderStatusTypes/useUpdateOrderStatusPriorities/index.ts +99 -0
- package/src/hooks/useOrders/index.ts +3 -0
- package/src/hooks/useOrders/queries.ts +99 -0
- package/src/hooks/useOrders/useChangeOrderState/index.ts +128 -0
- package/src/hooks/useOrders/useOrdersFromStore/index.ts +78 -0
- package/src/hooks/usePWAInstall/index.ts +38 -0
- package/src/hooks/usePaymentMethod/index.ts +3 -0
- package/src/hooks/usePaymentMethod/paymentMethod.gql.ts +62 -0
- package/src/hooks/usePaymentMethod/paymentMethod.types.ts +28 -0
- package/src/hooks/usePaymentMethod/useCreatePaymentMethod/index.ts +25 -0
- package/src/hooks/usePaymentMethod/useGetAllPaymentMethods/index.ts +19 -0
- package/src/hooks/usePaymentMethod/useGetPaymentMethod/index.ts +26 -0
- package/src/hooks/usePortFetcher/index.ts +33 -0
- package/src/hooks/usePrintSaleTicket/index.ts +68 -0
- package/src/hooks/useProductsFood/index.ts +341 -0
- package/src/hooks/useProductsFood/queriesStore.ts +964 -0
- package/src/hooks/useProductsFood/types/index.ts +0 -0
- package/src/hooks/useProductsFood/useEditProduct.ts +49 -0
- package/src/hooks/useProductsFood/usetagsProducts.ts +101 -0
- package/src/hooks/useProviders/index.ts +3 -0
- package/src/hooks/useProviders/queries.ts +31 -0
- package/src/hooks/useProviders/useProvidersCreateStore/index.ts +13 -0
- package/src/hooks/useProviders/useProvidersDataStore/index.ts +25 -0
- package/src/hooks/useProvidersStore/index.ts +25 -0
- package/src/hooks/useProvidersStore/queries.ts +31 -0
- package/src/hooks/usePushNotificationOrder/index.ts +52 -0
- package/src/hooks/usePushNotifications/helpers/index.ts +123 -0
- package/src/hooks/usePushNotifications/index.ts +150 -0
- package/src/hooks/useQueryLocationsMap/index.ts +20 -0
- package/src/hooks/useQueryLocationsMap/queries.ts +40 -0
- package/src/hooks/useRatingArrayData/index.ts +54 -0
- package/src/hooks/useRatingArrayData/queries.ts +19 -0
- package/src/hooks/useReactToPrint/index.txt +1223 -0
- package/src/hooks/useRemoveExtraProductFoodsOptional/index.ts +24 -0
- package/src/hooks/useRemoveExtraProductFoodsOptional/queries.ts +48 -0
- package/src/hooks/useReport/index.ts +48 -0
- package/src/hooks/useReport/queries.ts +72 -0
- package/src/hooks/useRestaurant/helpers/index.ts +28 -0
- package/src/hooks/useRestaurant/helpers/manageStatusOpen.ts +26 -0
- package/src/hooks/useRestaurant/index.ts +71 -0
- package/src/hooks/useRestaurant/queries.ts +81 -0
- package/src/hooks/useRoads/index.ts +20 -0
- package/src/hooks/useRoads/queries.ts +13 -0
- package/src/hooks/useRoles/index.ts +4 -0
- package/src/hooks/useRoles/queries.ts +70 -0
- package/src/hooks/useRoles/useCreateRole.ts +40 -0
- package/src/hooks/useRoles/useGetRoles.ts +37 -0
- package/src/hooks/useRoles/useRemoveRoles.ts +40 -0
- package/src/hooks/useRoles/useUpdateRolesPriority.ts +44 -0
- package/src/hooks/useSales/helpers/add-product.utils.ts +120 -0
- package/src/hooks/useSales/helpers/apply-discount-to-cart.utils.ts +292 -0
- package/src/hooks/useSales/helpers/comment-product.utils.ts +41 -0
- package/src/hooks/useSales/helpers/constants/index.ts +38 -0
- package/src/hooks/useSales/helpers/extras.utils.ts +42 -0
- package/src/hooks/useSales/helpers/filterProductsByCarProId.utils.ts +17 -0
- package/src/hooks/useSales/helpers/increment-product-quantity.utils.ts +76 -0
- package/src/hooks/useSales/helpers/index.ts +3 -0
- package/src/hooks/useSales/helpers/initializer.utils.ts +22 -0
- package/src/hooks/useSales/helpers/isStockInsufficient.ts +3 -0
- package/src/hooks/useSales/helpers/remove-product.utils.test.ts +49 -0
- package/src/hooks/useSales/helpers/remove-product.utils.ts +34 -0
- package/src/hooks/useSales/helpers/resolveProduct.ts +29 -0
- package/src/hooks/useSales/helpers/useAddToCart.ts +64 -0
- package/src/hooks/useSales/index.ts +1418 -0
- package/src/hooks/useSales/queries.ts +532 -0
- package/src/hooks/useSales/types/index.ts +206 -0
- package/src/hooks/useSales/types/use-sales.types.ts +8 -0
- package/src/hooks/useSales/useGetSale.ts +21 -0
- package/src/hooks/useSales/useTotalSales.ts +24 -0
- package/src/hooks/useSaveAvailableProduct/helpers/index.ts +38 -0
- package/src/hooks/useSaveAvailableProduct/index.ts +32 -0
- package/src/hooks/useSaveAvailableProduct/queries.ts +10 -0
- package/src/hooks/useSaveLocation/index.ts +17 -0
- package/src/hooks/useSaveLocation/queries.ts +19 -0
- package/src/hooks/useSchedule/index.ts +35 -0
- package/src/hooks/useSchedule/queries.ts +43 -0
- package/src/hooks/useSchedule/schedule.ts +79 -0
- package/src/hooks/useScheduleData/index.ts +135 -0
- package/src/hooks/useScroll/index.ts +56 -0
- package/src/hooks/useScrollRotate/index.ts +16 -0
- package/src/hooks/useSetImageProducts/index.ts +59 -0
- package/src/hooks/useSetImageProducts/queries.ts +18 -0
- package/src/hooks/useSetSession/index.ts +44 -0
- package/src/hooks/useSetState/index.ts +24 -0
- package/src/hooks/useSetupSchedule/helpers/index.ts +85 -0
- package/src/hooks/useSetupSchedule/index.ts +284 -0
- package/src/hooks/useStatusOpenStore/helpers/index.ts +124 -0
- package/src/hooks/useStatusOpenStore/index.ts +195 -0
- package/src/hooks/useStatusOrdersClient/helpers/index.ts +14 -0
- package/src/hooks/useStatusOrdersClient/index.ts +18 -0
- package/src/hooks/useStatusOrdersClient/queries.ts +80 -0
- package/src/hooks/useStock/index.ts +1 -0
- package/src/hooks/useStock/useStockUpdatedAllSubscription.ts +40 -0
- package/src/hooks/useStockMovements/helpers/index.ts +16 -0
- package/src/hooks/useStockMovements/index.ts +39 -0
- package/src/hooks/useStore/index.ts +83 -0
- package/src/hooks/useStore/queries.ts +163 -0
- package/src/hooks/useStoreCalendar/index.ts +5 -0
- package/src/hooks/useStoreContacts/index.ts +53 -0
- package/src/hooks/useStoreContacts/queries.ts +48 -0
- package/src/hooks/useStoreTable/index.ts +2 -0
- package/src/hooks/useStoreTable/queries.ts +47 -0
- package/src/hooks/useStoreTable/useStoreTableCreate.ts +77 -0
- package/src/hooks/useStoreTable/useStoreTables.ts +15 -0
- package/src/hooks/useSubscriptionValidation/index.ts +112 -0
- package/src/hooks/useTagProducts/index.ts +3 -0
- package/src/hooks/useTagProducts/useDeleteOneTag.ts +106 -0
- package/src/hooks/useTagProducts/useGetAllTags.ts +68 -0
- package/src/hooks/useTagProducts/useRegisterMultipleTags.ts +157 -0
- package/src/hooks/useTheme/index.ts +65 -0
- package/src/hooks/useTimeAgo/useTimeAgo.ts +39 -0
- package/src/hooks/useTokenCards/index.ts +45 -0
- package/src/hooks/useTopProductsMovements/index.ts +27 -0
- package/src/hooks/useTotalAllSales/index.ts +25 -0
- package/src/hooks/useTotalProductsInStock/index.ts +23 -0
- package/src/hooks/useTotalProductsSold/index.ts +23 -0
- package/src/hooks/useTotalProductsSolded/index.ts +20 -0
- package/src/hooks/useUpdateCart/index.ts +131 -0
- package/src/hooks/useUpdateCartCookie/index.ts +60 -0
- package/src/hooks/useUpdateDashboardComponent/index.ts +91 -0
- package/src/hooks/useUpdateExistingOrders/index.ts +85 -0
- package/src/hooks/useUpdateExtProductFoodsSubOptional/index.ts +61 -0
- package/src/hooks/useUpdateModuleOrder/index.ts +37 -0
- package/src/hooks/useUpdateMultipleExtProduct/index.ts +34 -0
- package/src/hooks/useUpdateMultipleExtProduct/queries.ts +33 -0
- package/src/hooks/useUpdateMultipleProducts/index.ts +117 -0
- package/src/hooks/useUpdateMultipleProducts/queries.ts +51 -0
- package/src/hooks/useUploadProducts/helpers/index.ts +1 -0
- package/src/hooks/useUploadProducts/helpers/parseNumber.ts +37 -0
- package/src/hooks/useUploadProducts/helpers/validateProductDataExcel.ts +73 -0
- package/src/hooks/useUploadProducts/index.ts +442 -0
- package/src/hooks/useUpsertGoal/index.ts +68 -0
- package/src/hooks/useUser/index.ts +24 -0
- package/src/hooks/useUser/queries.ts +131 -0
- package/src/hooks/useWeeklyStockMovement/helpers/index.ts +32 -0
- package/src/hooks/useWeeklyStockMovement/index.ts +52 -0
- package/src/hooks/useWindowSize/index.ts +45 -0
- package/src/index.ts +13 -0
- package/src/mock/dessert/index.ts +16 -0
- package/src/mock/index.ts +1 -0
- package/src/security/index.ts +1 -0
- package/src/services/index.ts +1 -0
- package/src/utils/UtilDateRange.ts +56 -0
- package/src/utils/generateCode.ts +222 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/utils.ts +344 -0
- package/tsconfig.eslint.json +16 -0
- package/tsconfig.json +21 -0
- package/tsup.config.ts +24 -0
- package/dist/index.cjs +0 -20089
|
@@ -0,0 +1,1418 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useApolloClient,
|
|
3
|
+
useLazyQuery,
|
|
4
|
+
useMutation
|
|
5
|
+
} from '@apollo/client'
|
|
6
|
+
import {
|
|
7
|
+
useCallback,
|
|
8
|
+
useEffect,
|
|
9
|
+
useReducer,
|
|
10
|
+
useMemo,
|
|
11
|
+
useState
|
|
12
|
+
} from 'react'
|
|
13
|
+
|
|
14
|
+
import { Cookies } from '../../cookies'
|
|
15
|
+
import { RandomCode, getCurrentDomain } from '../../utils'
|
|
16
|
+
import { generateTicket } from '../../utils/generateCode'
|
|
17
|
+
import { useCatWithProduct } from '../useCatWithProduct'
|
|
18
|
+
import { CatProductWithProduct, GetCatProductsWithProductResponse } from '../useCatWithProduct/types'
|
|
19
|
+
import { useFormatDate } from '../useFormatDate'
|
|
20
|
+
import { useLogout } from '../useLogout'
|
|
21
|
+
import { useGetOneProductsFood, useProductsFood } from '../useProductsFood'
|
|
22
|
+
import {
|
|
23
|
+
GET_ALL_EXTRA_PRODUCT,
|
|
24
|
+
GET_EXTRAS_PRODUCT_FOOD_OPTIONAL,
|
|
25
|
+
GET_ONE_PRODUCTS_FOOD
|
|
26
|
+
} from '../useProductsFood/queriesStore'
|
|
27
|
+
import { useStore } from '../useStore'
|
|
28
|
+
|
|
29
|
+
// import { updateExistingOrders } from '../useUpdateExistingOrders'
|
|
30
|
+
|
|
31
|
+
import { addToCartFunc } from './helpers/add-product.utils'
|
|
32
|
+
import { applyDiscountToCart } from './helpers/apply-discount-to-cart.utils'
|
|
33
|
+
import { initialStateSales, SalesActionTypes } from './helpers/constants'
|
|
34
|
+
import { decrementExtra } from './helpers/extras.utils'
|
|
35
|
+
import { filterProductsByCarProId } from './helpers/filterProductsByCarProId.utils'
|
|
36
|
+
import { handleRemoveProduct } from './helpers/remove-product.utils'
|
|
37
|
+
import { incrementProductQuantity } from './helpers/increment-product-quantity.utils'
|
|
38
|
+
import {
|
|
39
|
+
CREATE_SHOPPING_CARD_TO_USER_STORE,
|
|
40
|
+
GET_ALL_COUNT_SALES
|
|
41
|
+
} from './queries'
|
|
42
|
+
import { SalesReducerAction, SalesState, ValuesState } from './types'
|
|
43
|
+
import { UseSalesProps } from './types/use-sales.types'
|
|
44
|
+
import { initializer } from './helpers/initializer.utils'
|
|
45
|
+
import { useGetSale } from './useGetSale'
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
export const useSales = ({
|
|
49
|
+
disabled = false,
|
|
50
|
+
router,
|
|
51
|
+
sendNotification = (args) => { return args },
|
|
52
|
+
setAlertBox = (args) => { return args }
|
|
53
|
+
}: UseSalesProps) => {
|
|
54
|
+
const keyToSaveData = String(process.env.NEXT_LOCAL_SALES_STORE)
|
|
55
|
+
const saveDataState = JSON.parse(Cookies.get(keyToSaveData) ?? '[]')
|
|
56
|
+
const domain = getCurrentDomain()
|
|
57
|
+
const [loadingSale, setLoadingSale] = useState(false)
|
|
58
|
+
const [errorSale, setErrorSale] = useState(false)
|
|
59
|
+
const [onClickLogout] = useLogout({})
|
|
60
|
+
const [modalItem, setModalItem] = useState(false)
|
|
61
|
+
const [openCommentModal, setOpenCommentModal] = useState(false)
|
|
62
|
+
const [search, setSearch] = useState('')
|
|
63
|
+
const [categories, setCategories] = useState<CatProductWithProduct[]>([])
|
|
64
|
+
|
|
65
|
+
useCatWithProduct({
|
|
66
|
+
max: Infinity,
|
|
67
|
+
callback: (data: GetCatProductsWithProductResponse) => {
|
|
68
|
+
return setCategories(data.getCatProductsWithProduct?.catProductsWithProduct || [])
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
const [currentPage, setCurrentPage] = useState<number>(1)
|
|
72
|
+
const [totalProductPrice, setTotalProductPrice] = useState<number>(0)
|
|
73
|
+
const [showMore, setShowMore] = useState<number>(100)
|
|
74
|
+
const [inputValue, setInputValue] = useState<string>('')
|
|
75
|
+
|
|
76
|
+
const [_, setFilteredList] = useState([])
|
|
77
|
+
const [delivery, setDelivery] = useState<boolean>(false)
|
|
78
|
+
const [print, setPrint] = useState<boolean>(false)
|
|
79
|
+
const [errors, setErrors] = useState({})
|
|
80
|
+
|
|
81
|
+
const initialValuesState: ValuesState = {
|
|
82
|
+
change: '',
|
|
83
|
+
cliId: '',
|
|
84
|
+
comment: '',
|
|
85
|
+
tableId: '',
|
|
86
|
+
valueDelivery: ''
|
|
87
|
+
}
|
|
88
|
+
const [values, setValues] = useState<ValuesState>(initialValuesState)
|
|
89
|
+
|
|
90
|
+
const [dataStore] = useStore()
|
|
91
|
+
const { createdAt, idStore } = dataStore ?? {
|
|
92
|
+
createdAt: null,
|
|
93
|
+
idStore: null
|
|
94
|
+
}
|
|
95
|
+
const [code, setCode] = useState(null)
|
|
96
|
+
const [openCurrentSale, setOpenCurrentSale] = useState(null)
|
|
97
|
+
const [oneProductToComment, setOneProductToComment] = useState({})
|
|
98
|
+
const [sumExtraProducts, setSumExtraProducts] = useState(0)
|
|
99
|
+
const { yearMonthDay } = useFormatDate({ date: createdAt })
|
|
100
|
+
const [handleGetOneProduct] = useGetOneProductsFood()
|
|
101
|
+
const [valuesDates, setValuesDates] = useState(() => {
|
|
102
|
+
return { fromDate: yearMonthDay, toDate: '' }
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
const [product, setProduct] = useState({
|
|
106
|
+
PRODUCT: {
|
|
107
|
+
pId: null
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
const [loadingExtraProduct, setLoadingExtraProduct] = useState(false)
|
|
112
|
+
const [dataOptional, setDataOptional] = useState<any[]>([])
|
|
113
|
+
const [dataExtra, setDataExtra] = useState<any[]>([])
|
|
114
|
+
const [registerSalesStore, { loading: loadingRegisterSale }] = useMutation(
|
|
115
|
+
CREATE_SHOPPING_CARD_TO_USER_STORE,
|
|
116
|
+
{
|
|
117
|
+
onCompleted: (data) => {
|
|
118
|
+
const message = `${data?.registerSalesStore?.message}`
|
|
119
|
+
const error = data?.registerSalesStore?.success
|
|
120
|
+
? 'Éxito'
|
|
121
|
+
: 'Error'
|
|
122
|
+
sendNotification({
|
|
123
|
+
backgroundColor: error ? 'success' : 'error',
|
|
124
|
+
title: error,
|
|
125
|
+
description: message
|
|
126
|
+
})
|
|
127
|
+
setAlertBox({ message, type: 'success' })
|
|
128
|
+
if (message === 'Token expired') {
|
|
129
|
+
// @ts-ignore
|
|
130
|
+
onClickLogout()
|
|
131
|
+
}
|
|
132
|
+
setOpenCurrentSale(data?.registerSalesStore.success)
|
|
133
|
+
},
|
|
134
|
+
onError: (error) => {
|
|
135
|
+
sendNotification({
|
|
136
|
+
backgroundColor: 'error',
|
|
137
|
+
title: typeof error === 'string' ? error : 'Lo sentimos',
|
|
138
|
+
description: 'ha ocurrido un error'
|
|
139
|
+
})
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
const [productFoodsOne, { data: dataProduct }] = useLazyQuery(GET_ONE_PRODUCTS_FOOD)
|
|
145
|
+
|
|
146
|
+
const [ExtProductFoodsSubOptionalAll, { loading: loadingExtProductFoodsSubOptionalAll }] = useLazyQuery(
|
|
147
|
+
GET_EXTRAS_PRODUCT_FOOD_OPTIONAL,
|
|
148
|
+
{
|
|
149
|
+
onError: () => {
|
|
150
|
+
setDataOptional([])
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
)
|
|
154
|
+
const [ExtProductFoodsAll] = useLazyQuery(GET_ALL_EXTRA_PRODUCT, {
|
|
155
|
+
onError: () => {
|
|
156
|
+
setDataExtra([])
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
const [productsFood, {
|
|
161
|
+
loading,
|
|
162
|
+
fetchMore,
|
|
163
|
+
pagination,
|
|
164
|
+
refetch
|
|
165
|
+
}] = useProductsFood({
|
|
166
|
+
// @ts-ignore
|
|
167
|
+
search: search?.length >= 4 ? search : '',
|
|
168
|
+
gender: [],
|
|
169
|
+
desc: [],
|
|
170
|
+
categories: [],
|
|
171
|
+
toDate: valuesDates?.toDate,
|
|
172
|
+
fromDate: valuesDates?.fromDate,
|
|
173
|
+
max: showMore,
|
|
174
|
+
min: 0,
|
|
175
|
+
isShopppingCard: true,
|
|
176
|
+
idStore: idStore ?? '',
|
|
177
|
+
dataSale: (Array.isArray(saveDataState?.PRODUCT) && saveDataState?.PRODUCT) ?? [],
|
|
178
|
+
callback: () => { return }
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
const handlePageChange = (pageNumber: number) => {
|
|
182
|
+
setCurrentPage(pageNumber)
|
|
183
|
+
refetch({ page: pageNumber })
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Toggles the `checked` field of a category with maximum performance.
|
|
188
|
+
* @param {string} caId - Category ID to toggle.
|
|
189
|
+
*/
|
|
190
|
+
const handleChangeCheck = (caId: string) => {
|
|
191
|
+
setCategories((prev) => {
|
|
192
|
+
if (!prev || prev.length === 0) return prev
|
|
193
|
+
|
|
194
|
+
const index = prev.findIndex((item) => {return item.carProId === caId})
|
|
195
|
+
if (index === -1) return prev // nothing to update → no rerender
|
|
196
|
+
|
|
197
|
+
const target = prev[index]
|
|
198
|
+
|
|
199
|
+
const updatedItem = {
|
|
200
|
+
...target,
|
|
201
|
+
checked: !target.checked
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const newList = [...prev] // clone array ONCE
|
|
205
|
+
newList[index] = updatedItem
|
|
206
|
+
|
|
207
|
+
return newList
|
|
208
|
+
})
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
// HANDLESS
|
|
213
|
+
// FILTER PRODUCT DATA_DB
|
|
214
|
+
// @ts-ignore
|
|
215
|
+
const handlePrint = () => {
|
|
216
|
+
if (disabled) {
|
|
217
|
+
return sendNotification({
|
|
218
|
+
title: 'Error',
|
|
219
|
+
description: 'Esta es la descripción',
|
|
220
|
+
backgroundColor: 'error'
|
|
221
|
+
})
|
|
222
|
+
}
|
|
223
|
+
setPrint(!print)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const handleChangeFilter = (value: string): void => {
|
|
227
|
+
return setSearch(value)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const handleChange = (
|
|
231
|
+
e: React.ChangeEvent<HTMLInputElement> | { target: { name: string; value: any } },
|
|
232
|
+
error: boolean
|
|
233
|
+
) => {
|
|
234
|
+
const target: any = (e as any).target
|
|
235
|
+
const { name, value } = target
|
|
236
|
+
setErrors({ ...errors, [name]: error })
|
|
237
|
+
setValues((prevValues) => {return {
|
|
238
|
+
...prevValues,
|
|
239
|
+
[name]: value
|
|
240
|
+
}})
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const onChangeInput = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
244
|
+
return setValuesDates({ ...valuesDates, [e.target.name]: e.target.value })
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Toggles editing state of a product with maximum performance.
|
|
249
|
+
* @param {any} state - Current reducer state.
|
|
250
|
+
* @param {any} action - Payload with product ID.
|
|
251
|
+
* @returns {any} New updated state.
|
|
252
|
+
*/
|
|
253
|
+
const handleToggleEditingStatus = (state: any, action: any) => {
|
|
254
|
+
const pId = action?.payload?.pId
|
|
255
|
+
if (!pId) return state
|
|
256
|
+
|
|
257
|
+
const list = state.PRODUCT
|
|
258
|
+
const index = list.findIndex((item: any) => {return item.pId === pId})
|
|
259
|
+
|
|
260
|
+
// If no match, nothing changes → no re-render
|
|
261
|
+
if (index === -1) return state
|
|
262
|
+
|
|
263
|
+
const target = list[index]
|
|
264
|
+
|
|
265
|
+
const updatedItem = {
|
|
266
|
+
...target,
|
|
267
|
+
editing: !target.editing,
|
|
268
|
+
oldQuantity: target.ProQuantity ?? 0
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Clone the array only once
|
|
272
|
+
const newList = [...list]
|
|
273
|
+
newList[index] = updatedItem
|
|
274
|
+
|
|
275
|
+
return {
|
|
276
|
+
...state,
|
|
277
|
+
PRODUCT: newList
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
const handleChangeFilterProduct = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
283
|
+
const text = searchedInput(e.target.value)
|
|
284
|
+
if (text === undefined || text === '') {
|
|
285
|
+
return
|
|
286
|
+
}
|
|
287
|
+
const filteredData = handleList(text)
|
|
288
|
+
setFilteredList(filteredData)
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
const handleComment = (product: any) => {
|
|
292
|
+
if (product) {
|
|
293
|
+
setOneProductToComment(product)
|
|
294
|
+
setValues({
|
|
295
|
+
...values,
|
|
296
|
+
comment: product?.comment ?? ''
|
|
297
|
+
})
|
|
298
|
+
}
|
|
299
|
+
setOpenCommentModal(!openCommentModal)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Updates a single product inside the PRODUCT array with maximum performance.
|
|
304
|
+
* @param {any} state - Current state.
|
|
305
|
+
* @param {any} payload - Action payload.
|
|
306
|
+
* @returns {any} New updated state.
|
|
307
|
+
*/
|
|
308
|
+
const handleSuccessUpdateQuantity = (state: any, payload: any) => {
|
|
309
|
+
const pId = payload?.payload?.pId
|
|
310
|
+
if (!pId) return state
|
|
311
|
+
|
|
312
|
+
const list = state.PRODUCT
|
|
313
|
+
const index = list.findIndex((item: any) => {return item.pId === pId})
|
|
314
|
+
|
|
315
|
+
// If no match → no state changes → no rerender
|
|
316
|
+
if (index === -1) return state
|
|
317
|
+
|
|
318
|
+
const target = list[index]
|
|
319
|
+
const updatedItem = {
|
|
320
|
+
...target,
|
|
321
|
+
editing: false,
|
|
322
|
+
oldQuantity: target.ProQuantity ?? 0
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Clone array ONCE
|
|
326
|
+
const newList = [...list]
|
|
327
|
+
newList[index] = updatedItem
|
|
328
|
+
sendNotification({
|
|
329
|
+
title: 'Cantidad actualizada',
|
|
330
|
+
backgroundColor: 'success',
|
|
331
|
+
description: `La cantidad de ${target.pName} ha sido actualizada a ${updatedItem.ProQuantity}.`
|
|
332
|
+
})
|
|
333
|
+
return {
|
|
334
|
+
...state,
|
|
335
|
+
PRODUCT: newList
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Cancels the update of a product's quantity, resetting it to the previous value.
|
|
342
|
+
* @param {Object} state - The current state.
|
|
343
|
+
* @param {Object} payload - The payload containing the product ID.
|
|
344
|
+
* @returns {Object} - The new state with the updated product quantity and editing status.
|
|
345
|
+
*/
|
|
346
|
+
const handleCancelUpdateQuantity = (state: any, payload: any) => {
|
|
347
|
+
// Validación de `state`
|
|
348
|
+
if (!state || typeof state !== 'object') {
|
|
349
|
+
sendNotification({
|
|
350
|
+
title: 'Error',
|
|
351
|
+
backgroundColor: 'error',
|
|
352
|
+
description: 'Ha ocurrido un error al actualizar la cantidad del producto.'
|
|
353
|
+
})
|
|
354
|
+
return state // Retorna el estado sin cambios si es inválido.
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Validación de `PRODUCT`
|
|
358
|
+
if (!Array.isArray(state.PRODUCT)) {
|
|
359
|
+
sendNotification({
|
|
360
|
+
title: 'Error',
|
|
361
|
+
backgroundColor: 'error',
|
|
362
|
+
description: 'Ha ocurrido un error al actualizar la cantidad del producto.'
|
|
363
|
+
})
|
|
364
|
+
return state
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Validación de `payload`
|
|
368
|
+
const { pId } = payload.payload || {}
|
|
369
|
+
if (!pId) {
|
|
370
|
+
sendNotification({
|
|
371
|
+
title: 'Error',
|
|
372
|
+
backgroundColor: 'error',
|
|
373
|
+
description: 'Ha ocurrido un error al actualizar la cantidad del producto.'
|
|
374
|
+
})
|
|
375
|
+
return state // Retorna el estado sin cambios si falta `pId`.
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
return {
|
|
379
|
+
...state,
|
|
380
|
+
PRODUCT: state.PRODUCT.map((item: any) => {
|
|
381
|
+
// Validación de propiedades en cada item
|
|
382
|
+
if (item.pId === pId) {
|
|
383
|
+
if (typeof item.oldQuantity !== 'number' || typeof item.unitPrice !== 'number') {
|
|
384
|
+
sendNotification({
|
|
385
|
+
title: 'Error',
|
|
386
|
+
backgroundColor: 'error',
|
|
387
|
+
description: 'Ha ocurrido un error al actualizar la cantidad del producto.'
|
|
388
|
+
})
|
|
389
|
+
return item // Retorna el item sin cambios si las propiedades son inválidas.
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return {
|
|
393
|
+
...item,
|
|
394
|
+
editing: false,
|
|
395
|
+
ProQuantity: item.oldQuantity,
|
|
396
|
+
ProPrice: item.oldQuantity * item.unitPrice
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return item
|
|
401
|
+
})
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const paymentMethod = (state: any, action: any) => {
|
|
406
|
+
return {
|
|
407
|
+
...state,
|
|
408
|
+
payId: action.payload
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
const handleAddProduct = async (product: any) => {
|
|
414
|
+
const pId = String(product?.pId)
|
|
415
|
+
const memo = productsFood.find((item: any) => {return String(item.pId) === pId})
|
|
416
|
+
if (!memo) {
|
|
417
|
+
const response = await handleGetOneProduct({ pId })
|
|
418
|
+
if (response.data?.productFoodsOne == null) {return sendNotification({
|
|
419
|
+
title: 'Error',
|
|
420
|
+
backgroundColor: 'error',
|
|
421
|
+
description: 'No se pudo obtener el producto'
|
|
422
|
+
})}
|
|
423
|
+
const productData = response.data?.productFoodsOne ?? { pId: null }
|
|
424
|
+
return dispatch({
|
|
425
|
+
type: SalesActionTypes.ADD_TO_CART,
|
|
426
|
+
payload: productData
|
|
427
|
+
})
|
|
428
|
+
}
|
|
429
|
+
return dispatch({
|
|
430
|
+
type: SalesActionTypes.ADD_TO_CART,
|
|
431
|
+
payload: memo
|
|
432
|
+
})
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const PRODUCT = (state: any, action: any) => {
|
|
436
|
+
switch (action.type) {
|
|
437
|
+
case SalesActionTypes.ADD_TO_CART:
|
|
438
|
+
return addToCartFunc({
|
|
439
|
+
state,
|
|
440
|
+
action,
|
|
441
|
+
product: action.payload,
|
|
442
|
+
sendAlertStock,
|
|
443
|
+
sendNotification
|
|
444
|
+
})
|
|
445
|
+
case SalesActionTypes.ADD_PRODUCT:
|
|
446
|
+
return {
|
|
447
|
+
...state,
|
|
448
|
+
// eslint-disable-next-line
|
|
449
|
+
PRODUCT: [...state?.PRODUCT, action?.payload],
|
|
450
|
+
}
|
|
451
|
+
case SalesActionTypes.REMOVE_PRODUCT:
|
|
452
|
+
return handleRemoveProduct(state, action, productsFood)
|
|
453
|
+
case SalesActionTypes.REMOVE_PRODUCT_TO_CART:
|
|
454
|
+
return {
|
|
455
|
+
...state,
|
|
456
|
+
PRODUCT: state?.PRODUCT?.filter((t: any) => {
|
|
457
|
+
return t.pId !== action?.payload.pId
|
|
458
|
+
}),
|
|
459
|
+
counter: state.counter - action.payload.ProQuantity
|
|
460
|
+
}
|
|
461
|
+
case SalesActionTypes.ON_CHANGE: {
|
|
462
|
+
return handleChangeNumber(
|
|
463
|
+
state,
|
|
464
|
+
action,
|
|
465
|
+
productsFood,
|
|
466
|
+
sendNotification
|
|
467
|
+
)
|
|
468
|
+
}
|
|
469
|
+
case SalesActionTypes.UPDATE_SUCCESS_QUANTITY_EDITING_PRODUCT: {
|
|
470
|
+
return handleSuccessUpdateQuantity(state, action)
|
|
471
|
+
}
|
|
472
|
+
case SalesActionTypes.CANCEL_UPDATE_QUANTITY_EDITING_PRODUCT: {
|
|
473
|
+
return handleCancelUpdateQuantity(state, action)
|
|
474
|
+
}
|
|
475
|
+
case SalesActionTypes.REMOVE_ALL_PRODUCTS:
|
|
476
|
+
// @ts-ignore
|
|
477
|
+
setValues({
|
|
478
|
+
...values,
|
|
479
|
+
comment: '',
|
|
480
|
+
change: '',
|
|
481
|
+
valueDelivery: ''
|
|
482
|
+
})
|
|
483
|
+
return {
|
|
484
|
+
...state,
|
|
485
|
+
PRODUCT: [],
|
|
486
|
+
sortBy: null,
|
|
487
|
+
counter: 0
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
case SalesActionTypes.TOGGLE_FREE_PRODUCT:
|
|
491
|
+
return toggleFreeProducts(state, action)
|
|
492
|
+
case SalesActionTypes.TOGGLE_EDITING_PRODUCT: {
|
|
493
|
+
return handleToggleEditingStatus(state, action)
|
|
494
|
+
}
|
|
495
|
+
case SalesActionTypes.INCREMENT: {
|
|
496
|
+
return incrementProductQuantity({
|
|
497
|
+
state,
|
|
498
|
+
productId: action.id,
|
|
499
|
+
productsFood,
|
|
500
|
+
sendNotification
|
|
501
|
+
})
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
case SalesActionTypes.PUT_COMMENT:
|
|
505
|
+
return commentProducts(state, action)
|
|
506
|
+
case SalesActionTypes.PUT_EXTRA_PRODUCTS_AND_OPTIONAL_PRODUCT:
|
|
507
|
+
return handleUpdateAllExtraAndOptional(state, action)
|
|
508
|
+
case SalesActionTypes.PRICE_RANGE:
|
|
509
|
+
return {
|
|
510
|
+
...state,
|
|
511
|
+
priceRange: action.payload
|
|
512
|
+
}
|
|
513
|
+
case SalesActionTypes.SORT:
|
|
514
|
+
return { ...state, sortBy: action.payload }
|
|
515
|
+
case SalesActionTypes.DECREMENT:
|
|
516
|
+
return {
|
|
517
|
+
...state
|
|
518
|
+
}
|
|
519
|
+
case SalesActionTypes.PAYMENT_METHOD: return paymentMethod(state, action)
|
|
520
|
+
|
|
521
|
+
case SalesActionTypes.APPLY_DISCOUNT: {
|
|
522
|
+
return applyDiscountToCart(
|
|
523
|
+
state,
|
|
524
|
+
action.payload,
|
|
525
|
+
sendNotification
|
|
526
|
+
)
|
|
527
|
+
}
|
|
528
|
+
default:
|
|
529
|
+
return state
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
const [data, dispatch] = useReducer<React.Reducer<SalesState, SalesReducerAction>, SalesState>(PRODUCT, initialStateSales, initializer)
|
|
534
|
+
|
|
535
|
+
const handleRemoveValue = useCallback(({ name, value, pId }: { name: string; value: string | null; pId: string }) => {
|
|
536
|
+
setValues({
|
|
537
|
+
...values,
|
|
538
|
+
[name]: value ?? ''
|
|
539
|
+
})
|
|
540
|
+
sendNotification({
|
|
541
|
+
backgroundColor: 'success',
|
|
542
|
+
title: 'Comentario eliminado',
|
|
543
|
+
description: 'Has eliminado el comentario!'
|
|
544
|
+
})
|
|
545
|
+
// @ts-ignore
|
|
546
|
+
return dispatch({
|
|
547
|
+
type: SalesActionTypes.PUT_COMMENT,
|
|
548
|
+
payload: pId,
|
|
549
|
+
value: ''
|
|
550
|
+
})
|
|
551
|
+
}, [])
|
|
552
|
+
|
|
553
|
+
const handleAddOptional = ({ exOptional = null, codeCategory = null }) => {
|
|
554
|
+
if (!exOptional || !codeCategory) return
|
|
555
|
+
const item = dataOptional.find((item) => {return item.code === codeCategory})
|
|
556
|
+
if (!item) return
|
|
557
|
+
const idx = item.ExtProductFoodsSubOptionalAll.findIndex(
|
|
558
|
+
(el: any) => {return el.opSubExPid === exOptional}
|
|
559
|
+
)
|
|
560
|
+
if (item && idx !== -1) {
|
|
561
|
+
const updatedItem = {
|
|
562
|
+
// @ts-ignore
|
|
563
|
+
...item,
|
|
564
|
+
ExtProductFoodsSubOptionalAll: [
|
|
565
|
+
// @ts-ignore
|
|
566
|
+
...item.ExtProductFoodsSubOptionalAll.slice(0, idx),
|
|
567
|
+
{
|
|
568
|
+
// @ts-ignore
|
|
569
|
+
...item.ExtProductFoodsSubOptionalAll[idx],
|
|
570
|
+
// @ts-ignore
|
|
571
|
+
check: !item.ExtProductFoodsSubOptionalAll[idx].check
|
|
572
|
+
},
|
|
573
|
+
// @ts-ignore
|
|
574
|
+
...item.ExtProductFoodsSubOptionalAll.slice(idx + 1)
|
|
575
|
+
]
|
|
576
|
+
}
|
|
577
|
+
const newData = dataOptional.map((el) =>
|
|
578
|
+
// @ts-ignore
|
|
579
|
+
{return el.code === codeCategory ? updatedItem : el}
|
|
580
|
+
)
|
|
581
|
+
// @ts-ignore
|
|
582
|
+
setDataOptional(() => {return [...newData]})
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
*
|
|
588
|
+
* @param state
|
|
589
|
+
* @param action
|
|
590
|
+
*/
|
|
591
|
+
function handleUpdateAllExtraAndOptional(state: any, action: any) {
|
|
592
|
+
return {
|
|
593
|
+
...state,
|
|
594
|
+
PRODUCT: state?.PRODUCT?.map((items: any) => {
|
|
595
|
+
return items.pId === action.payload
|
|
596
|
+
? {
|
|
597
|
+
...items,
|
|
598
|
+
dataOptional: action.dataOptional || [],
|
|
599
|
+
dataExtra: action.dataExtra || []
|
|
600
|
+
}
|
|
601
|
+
: items
|
|
602
|
+
})
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/**
|
|
607
|
+
* Description
|
|
608
|
+
* @returns {any}
|
|
609
|
+
* */
|
|
610
|
+
useEffect(() => {
|
|
611
|
+
const arr =
|
|
612
|
+
dataExtra?.length > 0
|
|
613
|
+
? dataExtra?.filter((p) => {
|
|
614
|
+
// @ts-ignore
|
|
615
|
+
return p.quantity !== 0
|
|
616
|
+
})
|
|
617
|
+
: []
|
|
618
|
+
const val = arr.findIndex((item: any) => {return item.quantity !== 0})
|
|
619
|
+
if (val === -1) {
|
|
620
|
+
setSumExtraProducts(0)
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
*
|
|
624
|
+
*/
|
|
625
|
+
function sumNewExtraPrice() {
|
|
626
|
+
let sum = 0
|
|
627
|
+
arr.forEach((obj: any) => {
|
|
628
|
+
sum += obj.newExtraPrice ?? 0
|
|
629
|
+
})
|
|
630
|
+
if (arr.length === 0) {
|
|
631
|
+
setSumExtraProducts(0)
|
|
632
|
+
}
|
|
633
|
+
setSumExtraProducts(sum)
|
|
634
|
+
return sum
|
|
635
|
+
}
|
|
636
|
+
if (arr.length > 0) {
|
|
637
|
+
sumNewExtraPrice()
|
|
638
|
+
}
|
|
639
|
+
}, [dataExtra])
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
*
|
|
643
|
+
*/
|
|
644
|
+
function handleUpdateAllExtra() {
|
|
645
|
+
try {
|
|
646
|
+
if (!product?.PRODUCT?.pId) {
|
|
647
|
+
return sendNotification({
|
|
648
|
+
title: 'Error',
|
|
649
|
+
backgroundColor: 'error',
|
|
650
|
+
description: 'Ha ocurrido un error'
|
|
651
|
+
})
|
|
652
|
+
}
|
|
653
|
+
const filteredDataOptional = dataOptional
|
|
654
|
+
.map((obj) => {
|
|
655
|
+
// @ts-ignore
|
|
656
|
+
const filteredSubOptions = obj?.ExtProductFoodsSubOptionalAll?.filter(
|
|
657
|
+
(subObj: any) => {return subObj?.check === true}
|
|
658
|
+
)
|
|
659
|
+
// Excluya todo el objeto padre si filteredSubOptions está vacío
|
|
660
|
+
if (filteredSubOptions?.length === 0) {
|
|
661
|
+
return null
|
|
662
|
+
}
|
|
663
|
+
// @ts-ignore
|
|
664
|
+
return { ...obj, ExtProductFoodsSubOptionalAll: filteredSubOptions }
|
|
665
|
+
})
|
|
666
|
+
.filter((obj) => {return obj !== null}) // Elimine todos los objetos nulos del arreglo
|
|
667
|
+
// @ts-ignore
|
|
668
|
+
const filteredDataExtra = dataExtra?.filter((p) => {return p?.quantity !== undefined && p?.quantity !== 0})
|
|
669
|
+
if (product?.PRODUCT?.pId) {
|
|
670
|
+
// @ts-ignore
|
|
671
|
+
dispatch({
|
|
672
|
+
type: SalesActionTypes.PUT_EXTRA_PRODUCTS_AND_OPTIONAL_PRODUCT,
|
|
673
|
+
payload: product.PRODUCT.pId,
|
|
674
|
+
dataOptional: filteredDataOptional,
|
|
675
|
+
dataExtra: filteredDataExtra
|
|
676
|
+
})
|
|
677
|
+
const updatesOccurred = (
|
|
678
|
+
(dataExtra && dataExtra.length > 0)
|
|
679
|
+
)
|
|
680
|
+
if (updatesOccurred) {
|
|
681
|
+
return sendNotification({
|
|
682
|
+
title: 'Success',
|
|
683
|
+
backgroundColor: 'success',
|
|
684
|
+
description: 'Items subidos al producto'
|
|
685
|
+
})
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
} catch (_error) {
|
|
689
|
+
return sendNotification({
|
|
690
|
+
title: 'Error',
|
|
691
|
+
backgroundColor: 'error',
|
|
692
|
+
description: 'No se puedo actualizar el producto'
|
|
693
|
+
})
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
// @ts-ignore
|
|
698
|
+
/**
|
|
699
|
+
*
|
|
700
|
+
* @param root0
|
|
701
|
+
* @param root0.Adicionales
|
|
702
|
+
* @param root0.index
|
|
703
|
+
*/
|
|
704
|
+
function handleIncrementExtra({ Adicionales, index }) {
|
|
705
|
+
const { pId } = product?.PRODUCT || {}
|
|
706
|
+
const exPid = Adicionales?.exPid || null
|
|
707
|
+
|
|
708
|
+
if (exPid && pId) {
|
|
709
|
+
const newExtra = dataExtra.map((producto) => {
|
|
710
|
+
// @ts-ignore
|
|
711
|
+
if (exPid === producto.exPid) {
|
|
712
|
+
// @ts-ignore
|
|
713
|
+
const initialQuantity = producto?.quantity ? producto?.quantity : 0
|
|
714
|
+
const newQuantity = initialQuantity + 1
|
|
715
|
+
// @ts-ignore
|
|
716
|
+
const newExtraPrice = producto.extraPrice * newQuantity
|
|
717
|
+
|
|
718
|
+
return {
|
|
719
|
+
// @ts-ignore
|
|
720
|
+
...producto,
|
|
721
|
+
quantity: newQuantity,
|
|
722
|
+
newExtraPrice
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
return producto
|
|
726
|
+
})
|
|
727
|
+
|
|
728
|
+
// @ts-ignore
|
|
729
|
+
setDataExtra(newExtra)
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
const handleDecrementExtra = ({ Adicionales }: { Adicionales: any }) => {
|
|
733
|
+
const exPid = Adicionales?.exPid
|
|
734
|
+
if (!exPid) return
|
|
735
|
+
setDataExtra((prev) => {return decrementExtra(prev as any, exPid)})
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
/**
|
|
740
|
+
*
|
|
741
|
+
* @param stock
|
|
742
|
+
*/
|
|
743
|
+
function sendAlertStock(stock: number) {
|
|
744
|
+
return sendNotification({
|
|
745
|
+
title: 'Stock insuficiente',
|
|
746
|
+
backgroundColor: 'warning',
|
|
747
|
+
description: `Solo hay ${stock} unidades disponibles en el inventario`
|
|
748
|
+
})
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
/**
|
|
752
|
+
* Toggles free mode for a product and recalculates its price
|
|
753
|
+
* with maximum performance.
|
|
754
|
+
* @param {any} state - Current reducer state.
|
|
755
|
+
* @param {any} action - Payload containing pId.
|
|
756
|
+
* @returns {any} Updated state.
|
|
757
|
+
*/
|
|
758
|
+
function toggleFreeProducts(state: any, action: any) {
|
|
759
|
+
const pId = action?.payload?.pId
|
|
760
|
+
if (!pId) return state
|
|
761
|
+
|
|
762
|
+
// Find the reference product price only once
|
|
763
|
+
const referenceProduct = productsFood.find((item: any) => {return item.pId === pId})
|
|
764
|
+
if (!referenceProduct) return state
|
|
765
|
+
|
|
766
|
+
const list = state.PRODUCT
|
|
767
|
+
const index = list.findIndex((item: any) => {return item.pId === pId})
|
|
768
|
+
|
|
769
|
+
// If no match, skip re-render
|
|
770
|
+
if (index === -1) return state
|
|
771
|
+
|
|
772
|
+
const target = list[index]
|
|
773
|
+
|
|
774
|
+
const newFreeState = !target.free
|
|
775
|
+
|
|
776
|
+
const updatedItem = {
|
|
777
|
+
...target,
|
|
778
|
+
free: newFreeState,
|
|
779
|
+
ProPrice: newFreeState
|
|
780
|
+
? 0
|
|
781
|
+
: (target.ProQuantity ?? 1) * (referenceProduct.ProPrice ?? 0)
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
// Clone array ONCE
|
|
785
|
+
const newList = [...list]
|
|
786
|
+
newList[index] = updatedItem
|
|
787
|
+
|
|
788
|
+
return {
|
|
789
|
+
...state,
|
|
790
|
+
PRODUCT: newList
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
|
|
795
|
+
// COMMENT_FREE_PRODUCT
|
|
796
|
+
/**
|
|
797
|
+
*
|
|
798
|
+
* @param state
|
|
799
|
+
* @param action
|
|
800
|
+
* @param deleteValue
|
|
801
|
+
*/
|
|
802
|
+
function commentProducts(state: any, action: any, deleteValue = false) {
|
|
803
|
+
if (values.comment) {
|
|
804
|
+
sendNotification({
|
|
805
|
+
backgroundColor: 'success',
|
|
806
|
+
title: deleteValue ? 'Comentario eliminado' : 'Producto comentado',
|
|
807
|
+
description: deleteValue ? 'Has eliminado el comentario!' : 'Has comentado!'
|
|
808
|
+
})
|
|
809
|
+
}
|
|
810
|
+
setOpenCommentModal(!openCommentModal)
|
|
811
|
+
return {
|
|
812
|
+
...state,
|
|
813
|
+
PRODUCT: state?.PRODUCT?.map((items: any) => {
|
|
814
|
+
return items.pId === action.payload
|
|
815
|
+
? {
|
|
816
|
+
...items,
|
|
817
|
+
comment: deleteValue ? '' : values.comment
|
|
818
|
+
}
|
|
819
|
+
: items
|
|
820
|
+
})
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
const getSortedProduct = useCallback((data: any[], sort: string | null) => {
|
|
825
|
+
if (sort && sort === 'PRICE_HIGH_TO_LOW') {
|
|
826
|
+
return data.sort((a, b) => {
|
|
827
|
+
return b.ProPrice - a.ProPrice
|
|
828
|
+
})
|
|
829
|
+
}
|
|
830
|
+
if (sort && sort === 'PRICE_LOW_TO_HIGH') {
|
|
831
|
+
return data.sort((a, b) => {
|
|
832
|
+
return a.ProPrice - b.ProPrice
|
|
833
|
+
})
|
|
834
|
+
}
|
|
835
|
+
return data
|
|
836
|
+
}, [])
|
|
837
|
+
|
|
838
|
+
const PriceRangeFunc = (products: any[], price: number) => {
|
|
839
|
+
return (
|
|
840
|
+
products?.length > 0 &&
|
|
841
|
+
products?.filter((items) => {
|
|
842
|
+
return items?.ProPrice >= price
|
|
843
|
+
})
|
|
844
|
+
)
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
const sortedProduct = useMemo(() => {
|
|
848
|
+
return getSortedProduct(data.PRODUCT, data.sortBy)
|
|
849
|
+
}, [data.PRODUCT, data.sortBy, getSortedProduct])
|
|
850
|
+
|
|
851
|
+
const finalFilter = PriceRangeFunc(sortedProduct, data.priceRange)
|
|
852
|
+
|
|
853
|
+
const handleList = (text: string) => {
|
|
854
|
+
const inputText = text.toLowerCase()
|
|
855
|
+
let dataList = []
|
|
856
|
+
dataList = finalFilter.filter((item) => {
|
|
857
|
+
return item.pName.toLowerCase().includes(inputText)
|
|
858
|
+
})
|
|
859
|
+
return dataList
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
const searchedInput = (words: string) => {
|
|
863
|
+
setInputValue(words)
|
|
864
|
+
const n = words.split(' ')
|
|
865
|
+
if (n.length !== 0) {
|
|
866
|
+
if (n[n.length - 1] === '') {
|
|
867
|
+
n.pop()
|
|
868
|
+
}
|
|
869
|
+
return n[n.length - 1]
|
|
870
|
+
}
|
|
871
|
+
return ''
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
const arrayProduct = useMemo(() => {
|
|
875
|
+
if (!Array.isArray(data.PRODUCT) || data.PRODUCT.length === 0) return []
|
|
876
|
+
return data.PRODUCT.map((product: any) => {
|
|
877
|
+
const filteredDataExtra = (product?.dataExtra ?? []).map(({ __typename, ...rest }) => {return rest})
|
|
878
|
+
const dataOptional = (product?.dataOptional ?? []).map(({ __typename, ...rest }) => {
|
|
879
|
+
const { ExtProductFoodsSubOptionalAll, ...r } = rest
|
|
880
|
+
const adjusted = (ExtProductFoodsSubOptionalAll ?? []).map(({ __typename, ...s }) => {return s})
|
|
881
|
+
return { ...r, ExtProductFoodsSubOptionalAll: adjusted }
|
|
882
|
+
})
|
|
883
|
+
return {
|
|
884
|
+
pId: product.pId,
|
|
885
|
+
refCodePid: RandomCode(20),
|
|
886
|
+
id: values?.cliId,
|
|
887
|
+
cantProducts: parseInt(String(product?.ProQuantity ?? 0)),
|
|
888
|
+
comments: product?.comment ?? '',
|
|
889
|
+
dataOptional: dataOptional ?? [],
|
|
890
|
+
dataExtra: filteredDataExtra,
|
|
891
|
+
ProPrice: product.ProPrice
|
|
892
|
+
}
|
|
893
|
+
})
|
|
894
|
+
}, [data.PRODUCT, values?.cliId])
|
|
895
|
+
|
|
896
|
+
const finalArrayProduct = useMemo(() => {
|
|
897
|
+
return arrayProduct.map((item: any) => {
|
|
898
|
+
const totalExtra = (item.dataExtra ?? []).reduce((acc: number, ex: any) => {return acc + (Number(ex.newExtraPrice) || 0)}, 0)
|
|
899
|
+
return { ...item, totalExtra }
|
|
900
|
+
})
|
|
901
|
+
}, [arrayProduct])
|
|
902
|
+
|
|
903
|
+
let totalSale = 0
|
|
904
|
+
/**
|
|
905
|
+
*
|
|
906
|
+
* @param data
|
|
907
|
+
*/
|
|
908
|
+
function sumProPriceAndTotalExtra(data) {
|
|
909
|
+
return data.map((item) => {
|
|
910
|
+
const totalExtra = item.dataExtra.reduce((acc, curr) => {
|
|
911
|
+
const newExtraPrice = parseFloat(curr.newExtraPrice)
|
|
912
|
+
if (isNaN(newExtraPrice)) {
|
|
913
|
+
return acc
|
|
914
|
+
}
|
|
915
|
+
return acc + newExtraPrice
|
|
916
|
+
}, 0)
|
|
917
|
+
const total = item.ProPrice + totalExtra
|
|
918
|
+
return { ...item, totalExtra, total }
|
|
919
|
+
})
|
|
920
|
+
}
|
|
921
|
+
useEffect(() => {
|
|
922
|
+
const dataCountTotal = sumProPriceAndTotalExtra(finalArrayProduct)
|
|
923
|
+
dataCountTotal.forEach((a) => {
|
|
924
|
+
const { total } = a || {}
|
|
925
|
+
totalSale += total
|
|
926
|
+
setTotalProductPrice(Math.abs(totalSale))
|
|
927
|
+
})
|
|
928
|
+
if (data.PRODUCT.length === 0) {
|
|
929
|
+
setTotalProductPrice(0)
|
|
930
|
+
}
|
|
931
|
+
}, [totalProductPrice, totalSale, data, finalArrayProduct])
|
|
932
|
+
|
|
933
|
+
const client = useApolloClient()
|
|
934
|
+
const { getOneSalesStore } = useGetSale()
|
|
935
|
+
console.log(data)
|
|
936
|
+
const handleSubmit = () => {
|
|
937
|
+
// @ts-ignore
|
|
938
|
+
if (errors?.change || errors?.valueDelivery) {
|
|
939
|
+
return sendNotification({
|
|
940
|
+
title: 'error',
|
|
941
|
+
backgroundColor: 'warning',
|
|
942
|
+
description: 'Completa los campos requeridos'
|
|
943
|
+
})
|
|
944
|
+
}
|
|
945
|
+
setLoadingSale(true)
|
|
946
|
+
const {
|
|
947
|
+
code,
|
|
948
|
+
success,
|
|
949
|
+
error
|
|
950
|
+
} = generateTicket({
|
|
951
|
+
length: 8,
|
|
952
|
+
strategy: 'numeric',
|
|
953
|
+
prefix: 'T-',
|
|
954
|
+
timestamp: true
|
|
955
|
+
})
|
|
956
|
+
if (error) {
|
|
957
|
+
setLoadingSale(false)
|
|
958
|
+
return sendNotification({
|
|
959
|
+
title: 'error',
|
|
960
|
+
backgroundColor: 'error',
|
|
961
|
+
description: 'Lo sentimos, ocurrió un error'
|
|
962
|
+
})
|
|
963
|
+
}
|
|
964
|
+
if (!success) {
|
|
965
|
+
setLoadingSale(false)
|
|
966
|
+
return sendNotification({
|
|
967
|
+
title: 'error',
|
|
968
|
+
backgroundColor: 'error',
|
|
969
|
+
description: 'Lo sentimos, ocurrió un error'
|
|
970
|
+
})
|
|
971
|
+
}
|
|
972
|
+
// @ts-ignore
|
|
973
|
+
setCode(code)
|
|
974
|
+
/**
|
|
975
|
+
*
|
|
976
|
+
* @param cadena
|
|
977
|
+
*/
|
|
978
|
+
function convertInteger(cadena: string | number) {
|
|
979
|
+
if (typeof cadena === 'string') {
|
|
980
|
+
const numeroEntero = parseInt(cadena?.replace('.', ''))
|
|
981
|
+
return numeroEntero
|
|
982
|
+
}
|
|
983
|
+
return cadena || 0
|
|
984
|
+
}
|
|
985
|
+
const {
|
|
986
|
+
change,
|
|
987
|
+
valueDelivery,
|
|
988
|
+
tableId
|
|
989
|
+
} = values || {
|
|
990
|
+
change: 0,
|
|
991
|
+
valueDelivery: 0,
|
|
992
|
+
tableId: null,
|
|
993
|
+
cliId: null
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
const shoppingCartRefCode = `REF-${RandomCode(36)}`
|
|
997
|
+
|
|
998
|
+
const input = finalArrayProduct.map((item: any) => {
|
|
999
|
+
return {
|
|
1000
|
+
...item,
|
|
1001
|
+
shoppingCartRefCode
|
|
1002
|
+
}
|
|
1003
|
+
})
|
|
1004
|
+
return registerSalesStore({
|
|
1005
|
+
variables: {
|
|
1006
|
+
input,
|
|
1007
|
+
id: values?.cliId,
|
|
1008
|
+
pCodeRef: code,
|
|
1009
|
+
tableId,
|
|
1010
|
+
change: convertInteger(change),
|
|
1011
|
+
valueDelivery: convertInteger(valueDelivery),
|
|
1012
|
+
payId: data.payId,
|
|
1013
|
+
pickUp: 1,
|
|
1014
|
+
shoppingCartRefCode,
|
|
1015
|
+
discount: {
|
|
1016
|
+
type: String(data.discountType),
|
|
1017
|
+
value: 2
|
|
1018
|
+
},
|
|
1019
|
+
totalProductsPrice: convertInteger(totalProductsPrice) || 0
|
|
1020
|
+
},
|
|
1021
|
+
update(cache) {
|
|
1022
|
+
cache.modify({
|
|
1023
|
+
fields: {
|
|
1024
|
+
productFoodsAll(existingProductFoodsAll = []) {
|
|
1025
|
+
return existingProductFoodsAll
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
})
|
|
1029
|
+
}
|
|
1030
|
+
})
|
|
1031
|
+
.then((response) => {
|
|
1032
|
+
if (response) {
|
|
1033
|
+
const { data } = response
|
|
1034
|
+
const { registerSalesStore } = data ?? {}
|
|
1035
|
+
const { success } = registerSalesStore ?? {}
|
|
1036
|
+
if (success) {
|
|
1037
|
+
setPrint(false)
|
|
1038
|
+
client.query({
|
|
1039
|
+
query: GET_ALL_COUNT_SALES,
|
|
1040
|
+
fetchPolicy: 'network-only',
|
|
1041
|
+
// @ts-ignore
|
|
1042
|
+
onCompleted: (data) => {
|
|
1043
|
+
client.writeQuery({ query: GET_ALL_COUNT_SALES, data: { getTodaySales: data.countSales.todaySales } })
|
|
1044
|
+
}
|
|
1045
|
+
})
|
|
1046
|
+
setValues(initialValuesState)
|
|
1047
|
+
handleChange({ target: { name: 'tableId', value: '' } }, false)
|
|
1048
|
+
getOneSalesStore({
|
|
1049
|
+
variables: {
|
|
1050
|
+
pCodeRef: code || ''
|
|
1051
|
+
}
|
|
1052
|
+
}).then((responseSale: any) => {
|
|
1053
|
+
if (responseSale?.data?.getOneSalesStore) {
|
|
1054
|
+
const currentSale = responseSale?.data?.getOneSalesStore || {}
|
|
1055
|
+
const inComingCodeRef = currentSale?.pCodeRef || null
|
|
1056
|
+
if (!inComingCodeRef) return null
|
|
1057
|
+
// client.cache.modify({
|
|
1058
|
+
// fields: {
|
|
1059
|
+
// getAllOrdersFromStore (existingOrders = []) {
|
|
1060
|
+
// try {
|
|
1061
|
+
// const newGetAllOrdersFromStore = updateExistingOrders(existingOrders, inComingCodeRef, 4, currentSale)
|
|
1062
|
+
// return newGetAllOrdersFromStore
|
|
1063
|
+
// } catch (e) {
|
|
1064
|
+
// return existingOrders
|
|
1065
|
+
// }
|
|
1066
|
+
// }
|
|
1067
|
+
// }
|
|
1068
|
+
// })
|
|
1069
|
+
}
|
|
1070
|
+
})
|
|
1071
|
+
router.push(
|
|
1072
|
+
{
|
|
1073
|
+
query: {
|
|
1074
|
+
...router.query,
|
|
1075
|
+
saleId: code
|
|
1076
|
+
}
|
|
1077
|
+
},
|
|
1078
|
+
undefined,
|
|
1079
|
+
{ shallow: true }
|
|
1080
|
+
)
|
|
1081
|
+
}
|
|
1082
|
+
}
|
|
1083
|
+
setLoadingSale(false)
|
|
1084
|
+
})
|
|
1085
|
+
.catch(() => {
|
|
1086
|
+
setLoadingSale(false)
|
|
1087
|
+
setErrorSale(true)
|
|
1088
|
+
setPrint(false)
|
|
1089
|
+
sendNotification({
|
|
1090
|
+
title: 'error',
|
|
1091
|
+
backgroundColor: 'error',
|
|
1092
|
+
description: 'Lo sentimos, ocurrió un error'
|
|
1093
|
+
})
|
|
1094
|
+
})
|
|
1095
|
+
.finally(() => {
|
|
1096
|
+
setPrint(false)
|
|
1097
|
+
setLoadingSale(false)
|
|
1098
|
+
})
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
const handleProduct = async (PRODUCT: any) => {
|
|
1102
|
+
setLoadingExtraProduct(true)
|
|
1103
|
+
const { pId } = PRODUCT || {}
|
|
1104
|
+
try {
|
|
1105
|
+
const originalArray = data.PRODUCT.find((item: any) => {
|
|
1106
|
+
return item.pId === pId
|
|
1107
|
+
})
|
|
1108
|
+
// OPTIONAL
|
|
1109
|
+
productFoodsOne({ variables: { pId } })
|
|
1110
|
+
const optionalAll = await ExtProductFoodsSubOptionalAll({
|
|
1111
|
+
variables: { pId }
|
|
1112
|
+
})
|
|
1113
|
+
const optionalFetch = optionalAll.data.ExtProductFoodsOptionalAll
|
|
1114
|
+
setDataOptional(optionalFetch || [])
|
|
1115
|
+
const existOptionalCookies = originalArray?.dataOptional
|
|
1116
|
+
const filteredDataOptional = existOptionalCookies?.length
|
|
1117
|
+
? existOptionalCookies
|
|
1118
|
+
?.map((obj: any) => {
|
|
1119
|
+
const filteredSubOptions =
|
|
1120
|
+
obj.ExtProductFoodsSubOptionalAll.filter(
|
|
1121
|
+
(subObj: any) => {return subObj.check === true}
|
|
1122
|
+
)
|
|
1123
|
+
// Excluya todo el objeto padre si filteredSubOptions está vacío
|
|
1124
|
+
if (filteredSubOptions.length === 0) {
|
|
1125
|
+
return null
|
|
1126
|
+
}
|
|
1127
|
+
return {
|
|
1128
|
+
...obj,
|
|
1129
|
+
ExtProductFoodsSubOptionalAll: filteredSubOptions
|
|
1130
|
+
}
|
|
1131
|
+
})
|
|
1132
|
+
.filter((obj: any) => {return obj !== null})
|
|
1133
|
+
: []
|
|
1134
|
+
|
|
1135
|
+
// Actualizar optionalAll.data.ExtProductFoodsSubOptionalAll con los valores actualizados de originalArray2.ExtProductFoodsSubOptionalAll
|
|
1136
|
+
if (optionalFetch && filteredDataOptional) {
|
|
1137
|
+
const updateOption = optionalFetch
|
|
1138
|
+
.map((obj: any) => {
|
|
1139
|
+
const matchingArray = filteredDataOptional.find(
|
|
1140
|
+
(o: any) => {return o && o.opExPid === obj.opExPid}
|
|
1141
|
+
)
|
|
1142
|
+
if (!matchingArray) {
|
|
1143
|
+
return obj
|
|
1144
|
+
}
|
|
1145
|
+
const extProductFoodsSubOptionalAll =
|
|
1146
|
+
obj.ExtProductFoodsSubOptionalAll || []
|
|
1147
|
+
const updateExtProductSubOptionalAll =
|
|
1148
|
+
extProductFoodsSubOptionalAll.map((subObj: any) => {
|
|
1149
|
+
const newItem =
|
|
1150
|
+
matchingArray.ExtProductFoodsSubOptionalAll.find(
|
|
1151
|
+
(newItem: any) =>
|
|
1152
|
+
{return newItem && newItem.opSubExPid === subObj.opSubExPid}
|
|
1153
|
+
)
|
|
1154
|
+
if (newItem) {
|
|
1155
|
+
return {
|
|
1156
|
+
...subObj,
|
|
1157
|
+
check: true
|
|
1158
|
+
}
|
|
1159
|
+
}
|
|
1160
|
+
return subObj
|
|
1161
|
+
})
|
|
1162
|
+
return {
|
|
1163
|
+
...obj,
|
|
1164
|
+
ExtProductFoodsSubOptionalAll:
|
|
1165
|
+
updateExtProductSubOptionalAll
|
|
1166
|
+
}
|
|
1167
|
+
})
|
|
1168
|
+
.filter((obj: any) => {return obj})
|
|
1169
|
+
if (existOptionalCookies) {
|
|
1170
|
+
setDataOptional(updateOption || [])
|
|
1171
|
+
} else {
|
|
1172
|
+
setDataOptional(optionalAll.data.ExtProductFoodsOptionalAll || [])
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
// NO OPTIONAL
|
|
1176
|
+
const extProduct = await ExtProductFoodsAll({ variables: { pId } })
|
|
1177
|
+
let finalData
|
|
1178
|
+
if (!originalArray?.dataExtra) {
|
|
1179
|
+
finalData = extProduct?.data?.ExtProductFoodsAll
|
|
1180
|
+
} else {
|
|
1181
|
+
const filteredData = originalArray.dataExtra.filter((item: any) =>
|
|
1182
|
+
{return extProduct?.data?.ExtProductFoodsAll.some(
|
|
1183
|
+
(newItem: any) => {return newItem.exPid === item.exPid}
|
|
1184
|
+
)}
|
|
1185
|
+
)
|
|
1186
|
+
finalData = originalArray?.dataExtra?.concat(
|
|
1187
|
+
extProduct?.data?.ExtProductFoodsAll?.filter(
|
|
1188
|
+
(item: any) =>
|
|
1189
|
+
{return !filteredData?.some(
|
|
1190
|
+
(filteredItem: any) => {return filteredItem.exPid === item.exPid}
|
|
1191
|
+
)}
|
|
1192
|
+
)
|
|
1193
|
+
)
|
|
1194
|
+
}
|
|
1195
|
+
setDataExtra(finalData)
|
|
1196
|
+
setProduct(() => {
|
|
1197
|
+
return {
|
|
1198
|
+
PRODUCT
|
|
1199
|
+
}
|
|
1200
|
+
})
|
|
1201
|
+
setLoadingExtraProduct(false)
|
|
1202
|
+
} catch (error) {
|
|
1203
|
+
setLoadingExtraProduct(false)
|
|
1204
|
+
if (error instanceof Error) {
|
|
1205
|
+
return sendNotification({
|
|
1206
|
+
title: 'error',
|
|
1207
|
+
backgroundColor: 'error',
|
|
1208
|
+
description: error.message ?? 'Lo sentimos, ocurrió un error'
|
|
1209
|
+
})
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
const handleCleanFilter = (): null => {
|
|
1215
|
+
setValues(initialValuesState)
|
|
1216
|
+
setValuesDates({ fromDate: yearMonthDay, toDate: '' })
|
|
1217
|
+
return null
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
const disabledModalItems = (dataOptional?.length > 0 || dataExtra?.length > 0) && !loadingExtProductFoodsSubOptionalAll
|
|
1221
|
+
|
|
1222
|
+
/**
|
|
1223
|
+
* Filter objects with checked property equal to true.
|
|
1224
|
+
* @param {Array} products - Array of objects.
|
|
1225
|
+
* @returns {Array} - Array of objects with checked property equal to true.
|
|
1226
|
+
*/
|
|
1227
|
+
function filterChecked(products: any[]) {
|
|
1228
|
+
if (!Array.isArray(products)) {
|
|
1229
|
+
return []
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
return products.filter(product => {return product?.checked === true})?.map(product => {return product?.carProId})
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
// Obtener los carProIds de productos con checked en true
|
|
1236
|
+
const carProIds = filterChecked(categories)
|
|
1237
|
+
|
|
1238
|
+
// Filtrar los productos de productsFood por los carProIds obtenidos
|
|
1239
|
+
const filteredProducts = filterProductsByCarProId(productsFood, carProIds)
|
|
1240
|
+
|
|
1241
|
+
const allProducts = useMemo(() => {
|
|
1242
|
+
const productMap = new Map(data?.PRODUCT?.map((item: any) => {return [String(item.pId), item.ProQuantity || 0]}))
|
|
1243
|
+
|
|
1244
|
+
return filteredProducts.map(product => {return {
|
|
1245
|
+
...product,
|
|
1246
|
+
existsInSale: productMap.has(String(product.pId)),
|
|
1247
|
+
ProQuantity: productMap.get(String(product.pId)) || 0
|
|
1248
|
+
}})
|
|
1249
|
+
}, [data.PRODUCT, filteredProducts])
|
|
1250
|
+
|
|
1251
|
+
const totalProductsPrice = useMemo(() => { return finalArrayProduct.reduce((acc: number, item: any) => {return acc + (Number(item.ProPrice) || 0) + (Number(item.totalExtra) || 0)}, 0) }, [finalArrayProduct])
|
|
1252
|
+
|
|
1253
|
+
const handleAddAllProductsToCart = () => {
|
|
1254
|
+
for (const product of allProducts) {
|
|
1255
|
+
const existsInCart = data.PRODUCT.some((item: any) => {return item.pId === product.pId})
|
|
1256
|
+
if (!existsInCart) {
|
|
1257
|
+
dispatch({ type: SalesActionTypes.ADD_TO_CART, payload: product })
|
|
1258
|
+
} else {
|
|
1259
|
+
dispatch({ type: SalesActionTypes.INCREMENT, id: product.pId })
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
/**
|
|
1265
|
+
* handleChangeNumber
|
|
1266
|
+
* Updates product quantity from input change with full validations.
|
|
1267
|
+
* @param {any} state - Current reducer state
|
|
1268
|
+
* @param {any} action - Action with payload { value, index, id }
|
|
1269
|
+
* @param {any[]} productsFood - Products memory source
|
|
1270
|
+
* @param {(n:{title:string,backgroundColor:string,description?:string})=>void} sendNotification
|
|
1271
|
+
* @param {(a:any)=>void} dispatch
|
|
1272
|
+
* @returns {any} Updated state
|
|
1273
|
+
*/
|
|
1274
|
+
function handleChangeNumber(
|
|
1275
|
+
state: any,
|
|
1276
|
+
action: any,
|
|
1277
|
+
productsFood: any[] = [],
|
|
1278
|
+
sendNotification: Function
|
|
1279
|
+
) {
|
|
1280
|
+
const event = action?.payload ?? {}
|
|
1281
|
+
const { value, index, id } = event
|
|
1282
|
+
|
|
1283
|
+
if (!id || index === undefined) return state
|
|
1284
|
+
|
|
1285
|
+
const productExist = productsFood.find((item: any) => {return item.pId === id})
|
|
1286
|
+
const oneProduct = state?.PRODUCT?.find((item: any) => {return item.pId === id})
|
|
1287
|
+
|
|
1288
|
+
// Product not found
|
|
1289
|
+
if (!productExist || !oneProduct) return state
|
|
1290
|
+
|
|
1291
|
+
// No stock
|
|
1292
|
+
if (productExist.stock === 0) {
|
|
1293
|
+
sendNotification({
|
|
1294
|
+
title: 'Sin stock',
|
|
1295
|
+
backgroundColor: 'warning',
|
|
1296
|
+
description: 'Producto sin stock disponible'
|
|
1297
|
+
})
|
|
1298
|
+
return state
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
// Remove product if quantity <= 0
|
|
1302
|
+
if (value <= 0) {
|
|
1303
|
+
sendNotification({
|
|
1304
|
+
title: 'Producto eliminado',
|
|
1305
|
+
backgroundColor: 'info',
|
|
1306
|
+
description: `Has eliminado ${oneProduct.pName} del carrito.`
|
|
1307
|
+
})
|
|
1308
|
+
return {
|
|
1309
|
+
...state,
|
|
1310
|
+
PRODUCT: state.PRODUCT.filter((t: any) => {return t.pId !== id}),
|
|
1311
|
+
counter: (state.counter ?? 0) - (oneProduct.ProQuantity || 0)
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
const safeValue = Number(value) || 0
|
|
1316
|
+
const maxStock = productExist.manageStock ? productExist.stock : safeValue
|
|
1317
|
+
const finalQuantity = Math.min(safeValue, maxStock)
|
|
1318
|
+
|
|
1319
|
+
// Stock exceeded
|
|
1320
|
+
if (safeValue > productExist.stock && productExist.manageStock) {
|
|
1321
|
+
sendNotification({
|
|
1322
|
+
title: 'Stock insuficiente',
|
|
1323
|
+
backgroundColor: 'warning',
|
|
1324
|
+
description: `No puedes agregar más unidades de ${oneProduct.pName}. Stock disponible: ${productExist.stock}`
|
|
1325
|
+
})
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
const updatedProducts = state.PRODUCT.map((item: any, i: number) =>
|
|
1329
|
+
{return i === index
|
|
1330
|
+
? {
|
|
1331
|
+
...item,
|
|
1332
|
+
ProQuantity: finalQuantity,
|
|
1333
|
+
ProPrice: finalQuantity * (productExist.ProPrice ?? 0)
|
|
1334
|
+
}
|
|
1335
|
+
: item}
|
|
1336
|
+
)
|
|
1337
|
+
|
|
1338
|
+
return {
|
|
1339
|
+
...state,
|
|
1340
|
+
PRODUCT: updatedProducts,
|
|
1341
|
+
counter: (state.counter ?? 0) + 1
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
useEffect(() => {
|
|
1346
|
+
Cookies.set(keyToSaveData, JSON.stringify(data), { domain, path: '/' })
|
|
1347
|
+
}, [data, domain])
|
|
1348
|
+
|
|
1349
|
+
return {
|
|
1350
|
+
loading: loading || loadingSale,
|
|
1351
|
+
loadingExtraProduct,
|
|
1352
|
+
disabledModalItems: !disabledModalItems,
|
|
1353
|
+
loadingRegisterSale,
|
|
1354
|
+
errorSale,
|
|
1355
|
+
openCurrentSale,
|
|
1356
|
+
code,
|
|
1357
|
+
totalProductPrice,
|
|
1358
|
+
saveDataState,
|
|
1359
|
+
product,
|
|
1360
|
+
data,
|
|
1361
|
+
openCommentModal,
|
|
1362
|
+
inputValue,
|
|
1363
|
+
arrayProduct,
|
|
1364
|
+
delivery,
|
|
1365
|
+
valuesDates,
|
|
1366
|
+
print,
|
|
1367
|
+
finalFilter,
|
|
1368
|
+
showMore,
|
|
1369
|
+
search,
|
|
1370
|
+
values,
|
|
1371
|
+
initialStateSales,
|
|
1372
|
+
productsFood: allProducts,
|
|
1373
|
+
modalItem,
|
|
1374
|
+
sumExtraProducts,
|
|
1375
|
+
oneProductToComment: oneProductToComment ?? null,
|
|
1376
|
+
dataProduct: dataProduct?.productFoodsOne || {},
|
|
1377
|
+
dataOptional: dataOptional || [],
|
|
1378
|
+
dataExtra: dataExtra || [],
|
|
1379
|
+
fetchMore,
|
|
1380
|
+
pagination,
|
|
1381
|
+
discount: 0,
|
|
1382
|
+
datCat: categories,
|
|
1383
|
+
currentPage,
|
|
1384
|
+
loadingProduct: loading,
|
|
1385
|
+
handleChangeCheck,
|
|
1386
|
+
errors,
|
|
1387
|
+
handleUpdateAllExtra,
|
|
1388
|
+
handleAddAllProductsToCart,
|
|
1389
|
+
dispatch,
|
|
1390
|
+
handlePageChange,
|
|
1391
|
+
handleComment,
|
|
1392
|
+
setModalItem,
|
|
1393
|
+
handleChangeFilter,
|
|
1394
|
+
handleProduct,
|
|
1395
|
+
handleChange,
|
|
1396
|
+
setOpenCurrentSale,
|
|
1397
|
+
setErrors,
|
|
1398
|
+
onChangeInput,
|
|
1399
|
+
handleAddProduct,
|
|
1400
|
+
handleRemoveValue,
|
|
1401
|
+
setDelivery,
|
|
1402
|
+
setValues,
|
|
1403
|
+
setShowMore,
|
|
1404
|
+
PriceRangeFunc,
|
|
1405
|
+
handleCleanFilter,
|
|
1406
|
+
handleSubmit,
|
|
1407
|
+
handleChangeFilterProduct,
|
|
1408
|
+
handleDecrementExtra,
|
|
1409
|
+
setTotalProductPrice,
|
|
1410
|
+
setInputValue,
|
|
1411
|
+
getSortedProduct,
|
|
1412
|
+
handleAddOptional,
|
|
1413
|
+
handleIncrementExtra,
|
|
1414
|
+
setProduct,
|
|
1415
|
+
setPrint: handlePrint,
|
|
1416
|
+
PRODUCT
|
|
1417
|
+
}
|
|
1418
|
+
}
|