diff --git a/src/previews/csvf.rs b/src/previews/csvf.rs index adea1f8..020efe0 100644 --- a/src/previews/csvf.rs +++ b/src/previews/csvf.rs @@ -50,6 +50,10 @@ impl Table { .collect::>(); } + pub fn set_headers(&mut self, headers: Vec) { + self.header = headers; + } + pub fn get_current_header(self) -> Vec { self.header } diff --git a/src/recipe_functions/import.rs b/src/recipe_functions/import.rs index 28c0001..b7ff5cf 100644 --- a/src/recipe_functions/import.rs +++ b/src/recipe_functions/import.rs @@ -5,7 +5,7 @@ use serde_json::{Number, Value}; use super::common::*; use crate::{ - models::recipe::{self, PartialRecipe}, + models::recipe::{self, PartialRecipe, Recipe}, previews::{csvf::*, header::Headers}, }; @@ -353,17 +353,18 @@ pub fn generate_recipe_sheet_table(country_name: &str, into_version: usize) { recipe_table.set_config(insert_later_config); - match country_name { - "sgp" => { - info!("SGP last update [24.02.2025]"); - recipe_table.set_header(Headers::get_recipe_table_sgp_24022025()); - } - "dubai" => { - info!("UAE last update [16.05.2025]"); - recipe_table.set_header(Headers::get_recipe_table_dubai_16052025()); - } - _ => {} - } + // auto detect + // + let mut mat_header = vec!["Name".to_string(), "ProductCode".to_string()]; + mat_header.extend( + latest_recipe + .MaterialSetting + .iter() + .map(|ms| ms.id.as_u64().unwrap().to_string()) + .collect::>(), + ); + + recipe_table.set_headers(mat_header); let mut material_names = Vec::new(); @@ -453,16 +454,16 @@ pub fn generate_recipe_sheet_table(country_name: &str, into_version: usize) { if header_idx > 1 && header.eq(¤t_material.to_string()) { // search mat settings for further info - if rpl.productCode.clone().ends_with("0090") { - debug!( - "MAT [{}]: {} --> {} = pow:{:?},sy:{:?}", - rpl.productCode.clone(), - header_idx, - repl.materialPathId.clone(), - repl.powderGram, - repl.syrupGram - ); - } + // if rpl.productCode.clone().ends_with("0090") { + // debug!( + // "MAT [{}]: {} --> {} = pow:{:?},sy:{:?}", + // rpl.productCode.clone(), + // header_idx, + // repl.materialPathId.clone(), + // repl.powderGram, + // repl.syrupGram + // ); + // } let mat_setting = latest_recipe .search_material_settings(current_material.to_string()); @@ -488,27 +489,27 @@ pub fn generate_recipe_sheet_table(country_name: &str, into_version: usize) { } recipe::MaterialType::Syrup => { // debug - if rpl.productCode.clone().ends_with("0090") { - println!( - "SYRUP [{}]: {} --> {} = {:?}", - rpl.productCode.clone(), - header_idx, - repl.materialPathId.clone(), - repl.syrupGram - ); - } + // if rpl.productCode.clone().ends_with("0090") { + // println!( + // "SYRUP [{}]: {} --> {} = {:?}", + // rpl.productCode.clone(), + // header_idx, + // repl.materialPathId.clone(), + // repl.syrupGram + // ); + // } recipe_brewing_values_only[header_idx] += repl.syrupGram.as_i64().unwrap(); } recipe::MaterialType::Soda => { - println!( - "SODA [{}]: {} --> {} = {:?}", - rpl.productCode.clone(), - header_idx, - repl.materialPathId.clone(), - repl.syrupGram - ); + // println!( + // "SODA [{}]: {} --> {} = {:?}", + // rpl.productCode.clone(), + // header_idx, + // repl.materialPathId.clone(), + // repl.syrupGram + // ); recipe_brewing_values_only[header_idx] += repl.syrupGram.as_i64().unwrap(); @@ -590,3 +591,48 @@ pub fn generate_recipe_sheet_table(country_name: &str, into_version: usize) { recipe_table.add_column_desciption(0, material_names); recipe_table.generate_save_file("./test_result/recipe.csv"); } + +/// import only new data from source to target +/// +/// `import_target` must be mutable +/// +/// This only update the following fields +/// +/// - Recipe01 +/// - MaterialSetting +/// - MaterialCode +/// - Topping +/// +pub fn full_import_data(source: Recipe, mut import_target: Recipe) { + // import menus + source.Recipe01.iter().for_each(|r01| { + if !import_target.Recipe01.contains(r01) { + import_target.Recipe01.push(r01.clone()); + } + }); + + // import materials + source.MaterialSetting.iter().for_each(|ms| { + if !import_target.MaterialSetting.contains(ms) { + import_target.MaterialSetting.push(ms.clone()); + } + }); + + source.MaterialCode.iter().for_each(|mc| { + if !import_target.MaterialCode.contains(mc) { + import_target.MaterialCode.push(mc.clone()); + } + }); + + // import new toppings + source.Topping.ToppingList.iter().for_each(|tpl| { + if !import_target.Topping.ToppingList.contains(tpl) { + import_target.Topping.ToppingList.push(tpl.clone()); + } + }); + source.Topping.ToppingGroup.iter().for_each(|tg| { + if !import_target.Topping.ToppingGroup.contains(tg) { + import_target.Topping.ToppingGroup.push(tg.clone()); + } + }); +}