refactor: Atualiza a função process_sheet para gerar linhas E250 por dia com valores não-zero
This commit is contained in:
+12
-24
@@ -86,10 +86,8 @@ pub fn datatype_to_val_str_from_str(s: &str) -> Option<String> {
|
|||||||
/// Assumimos que a coluna 39 (zero-based 39 -> 40a coluna) contém o valor, e coluna 1 contém a data
|
/// Assumimos que a coluna 39 (zero-based 39 -> 40a coluna) contém o valor, e coluna 1 contém a data
|
||||||
/// Processa uma sheet específica do calamine (Range<DataType>) e retorna linhas E250
|
/// Processa uma sheet específica do calamine (Range<DataType>) e retorna linhas E250
|
||||||
pub fn process_sheet<T: std::fmt::Display + calamine::CellType>(range: &Range<T>, codigo_receita: &str, mes_apuracao: &str) -> Vec<String> {
|
pub fn process_sheet<T: std::fmt::Display + calamine::CellType>(range: &Range<T>, codigo_receita: &str, mes_apuracao: &str) -> Vec<String> {
|
||||||
// Agrega os valores da planilha por estado e retorna UM único |E250| com o total
|
// Gera uma linha |E250| para cada dia (linha) da planilha com valor não-zero
|
||||||
let mut total: f64 = 0.0;
|
let mut result: Vec<String> = Vec::new();
|
||||||
let mut any = false;
|
|
||||||
let mut chosen_vencimento: Option<String> = None;
|
|
||||||
|
|
||||||
for row in range.rows().skip(1) {
|
for row in range.rows().skip(1) {
|
||||||
let valor_dt_opt = if row.len() > 39 { row.get(39) } else { None };
|
let valor_dt_opt = if row.len() > 39 { row.get(39) } else { None };
|
||||||
@@ -106,32 +104,22 @@ pub fn process_sheet<T: std::fmt::Display + calamine::CellType>(range: &Range<T>
|
|||||||
|
|
||||||
// tentar parse do valor formatado (ex: "1.234,56" ou "1234,56")
|
// tentar parse do valor formatado (ex: "1.234,56" ou "1234,56")
|
||||||
let normalized = valor_str.replace('.', "").replace(',', ".");
|
let normalized = valor_str.replace('.', "").replace(',', ".");
|
||||||
if let Ok(v) = normalized.parse::<f64>() {
|
if let Ok(_v) = normalized.parse::<f64>() {
|
||||||
total += v;
|
// obter o vencimento desta linha específica
|
||||||
any = true;
|
let venc = vencimento_dt_opt
|
||||||
}
|
|
||||||
|
|
||||||
// escolher vencimento: a primeira data válida encontrada
|
|
||||||
if chosen_vencimento.is_none() {
|
|
||||||
if let Some(venc_dt) = vencimento_dt_opt
|
|
||||||
.map(|v| v.to_string())
|
.map(|v| v.to_string())
|
||||||
.and_then(|s| datatype_to_val_str_from_str(&s[..]))
|
.and_then(|s| datatype_to_val_str_from_str(&s[..]))
|
||||||
{
|
.unwrap_or_else(|| "01012000".to_string());
|
||||||
chosen_vencimento = Some(venc_dt);
|
|
||||||
}
|
// formatar valor para o SPED (vírgula decimal)
|
||||||
|
let valor_formatted = valor_str.replace('.', ",");
|
||||||
|
let linha = format!("|E250|999|{}|{}|{}|||||{}|", valor_formatted, venc, codigo_receita, mes_apuracao.replace('/', ""));
|
||||||
|
result.push(linha);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !any {
|
result
|
||||||
return Vec::new();
|
|
||||||
}
|
|
||||||
|
|
||||||
// formatar total para o SPED (vírgula decimal)
|
|
||||||
let valor_formatted = format!("{:.2}", total).replace('.', ",");
|
|
||||||
let venc = chosen_vencimento.unwrap_or_else(|| "01012000".to_string());
|
|
||||||
let linha = format!("|E250|999|{}|{}|{}|||||{}|", valor_formatted, venc, codigo_receita, mes_apuracao.replace('/', ""));
|
|
||||||
vec![linha]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Processa uma única linha representada por slice de Strings (útil para testes).
|
/// Processa uma única linha representada por slice de Strings (útil para testes).
|
||||||
|
|||||||
Reference in New Issue
Block a user