Check NAs (taken from loop_Ventas)

#check which columns have Nas
nacols <- function(df) {
colnames(df)[unlist(lapply(df, function(x) anyNA(x)))]
}
nacols(pre_Vtas_dtll_mov_full)

 #check NAs #updated
NAs_pre_Vtas_dtll_mov_full <-   pre_Vtas_dtll_mov_full %>% filter_all(any_vars( is.na(.)))

 data %>% drop_na() 

#Replace NAs by Col
summarized_285_Ventas$Ventas_Trimestrales[is.na(summarized_285_Ventas$Ventas_Trimestrales)] <-  0.001


#Replace NAs all Cols
TL_Vtas_dtll_mov_Establ_noSufixCostoNo0[is.na(TL_Vtas_dtll_mov_Establ_noSufixCostoNo0)] <- 0


#Replace NAs across all Numeric Cols  with a 0
dat %>% mutate(across(where(is.numeric), function(x) tidyr::replace_na(x, 0)))




#Replace a string
StockMin_285_max_replaced$StockMax_Trimestral <-  trimws(gsub("(2)", "3", StockMin_285_max_replaced$StockMax_Trimestral))

#Assign column names
names(pre_dataDespachos_cab_1) <- pre_dataDespachos_cab_1[1,]

names(dataDespachos_cab)[names(dataDespachos_cab) == "Nº GENERADO"] <- "N_DESPACHO"


#Separate Monofocales from Bi/Multifocales

listado_existencias_Series_Mono <- listado_existencias_Series %>% filter(FORMA %in% c("ESF", "CIL", "CMB"))

listado_existencias_Series_Multi <- listado_existencias_Series %>% filter(FORMA %in% c("BFT", "INV", "MFC", "MFL")) %>% mutate(MATERIAL=paste(MATERIAL,FORMA))

listado_existencias_Series_All  <- bind_rows(listado_existencias_Series_Mono, listado_existencias_Series_Multi)


#Remove info with #match
Temp_match0_dtll <- which(tb_bind_ventas_dtll_distinct$ESTABLECIMIENTO=="Laboratorio A&C")
#Re-create variable: Only Distrib
tb_bind_ventas_dtll_distinct_OnlyDistrib <- tb_bind_ventas_dtll_distinct %>% slice(-Temp_match0_dtll)


#Add new field SERIE and fill it considering ESF_CIL and CMB_ADD
  mutate(SERIE = case_when( 
#esf+ serie 1
    FORMA == "ESF" &  ESF_CIL >= 0 & ESF_CIL <= 4 ~ "ESFpos_1",
(…)
TRUE ~ "other"
  )
  )

count

  only22_PeriodLast_uniques <- filtered_sum_salidas_only22_PeriodLast %>% group_by(CODIGO_ARTICULO) %>% summarise (n = n())

GS_Cols_Kardex_Almacen_class_years2122_filtered4MQ2_22 %>% count(CODIGO_ARTICULO)




#for(i in 1:length(list_285_byTL)) {                    # assign function within loop
 #assign(paste0("tb_285_", i), list_285_byTL[[i]])
#}


list_285_byTL = split(col_print1_medidas_y_stk_min_285_max_replaced, col_print1_medidas_y_stk_min_285_max_replaced$MATERIAL)





#order


#wide or long
wide_exist_adhoc_Category_Stock_Estabs <- li_exist_adhoc_Category_Stock_Estabs %>%
  pivot_wider(names_from = Establecimiento, values_from = `STOCK ACTUAL`)



#totals (adorn)



###############################################################################
#                               Saving       #
###############################################################################

#Path
pathToSave <- "~/Documents/@/CasaDelOptico2023/Analisis/scripts/"


#R file
#--Stock
#saveRDS(Ventas_AllEstab_AllCat, file= paste(pathToSave, "loop_Ventas_detalle_Cab_allDistrib_allCat_01_01_23-29_04_23_v1.rds", sep = ""))

#Excel
#write.xlsx(Ventas_AllEstab_AllCat, file= paste(pathToSave, "loop_Ventas_detalle_cat_allDistrib_allCat_01_01_23-29_04_23_v1.xlsx", sep = ""), colNames=TRUE, rowNames = FALSE,showNA = TRUE)