func NewSheet(s string, o osfile.OsFile) Sheet { name, err1 := NewFilename(s) if err1 != nil { log.WithFields(log.Fields{ "path": o.Dir, "filename": name.String(), "err": err1, }).Error(asset.Tr("error_file_csv_unmarshal")) } r := o.NewReader(name.String()) defer r.Close() rcsv := csv.NewReader(r) out, err2 := rcsv.ReadAll() if err2 != nil { log.WithFields(log.Fields{ "path": o.Dir, "filename": name.String(), "view": out, "err": err2, }).Error(asset.Tr("error_file_csv_unmarshal")) } tab := strtab.NewT(out[0], out[1:]...) return Sheet{name, tab, nil} }
func NewFilename(s string) (Filename, error) { var status string args := strings.Split(s, sep) if l := len(args); l != 5 && l != 6 { return Filename{}, fmt.Errorf(asset.Tr("filename_number_argument_error"), s) } if len(args) == 6 { status = args[0] args = args[1:] } date, err := time.Parse(timeFormat, args[0]+sep+args[1]+sep+args[2]) if err != nil { return Filename{}, fmt.Errorf(asset.Tr("filename_date_parse_error"), s) } return Filename{status, date, strings.TrimPrefix(args[3], numberPrefix), args[4], ""}, nil }
func (o OsFile) NewWriter(name string) io.WriteCloser { w, err := os.Create(o.Dir + name + extension) if err != nil { log.WithFields(log.Fields{ "filepath": o.Dir + name + extension, "err": err, }).Error(asset.Tr("create_file_error")) } return w }
func (o OsFile) NewReader(name string) io.ReadCloser { r, err := os.OpenFile(o.Dir+name+extension, os.O_CREATE, 0666) if err != nil { log.WithFields(log.Fields{ "filepath": o.Dir + name + extension, "err": err, }).Error(asset.Tr("error_file_open")) } return r }
func AllSheets(o osfile.OsFile) []string { var fs []string list := o.GetAll() for _, s := range list { if strings.HasPrefix(s, generatedPrefix) || strings.HasPrefix(s, asset.Tr("file_prefix_draft")) { continue } fs = append(fs, s) } return fs }
func (s Sheet) Put(o osfile.OsFile) { w := o.NewWriter(s.Name.String()) defer w.Close() wcsv := csv.NewWriter(w) wcsv.WriteAll(s.Render(s.Table)) if wcsv.Error() != nil { log.WithFields(log.Fields{ "path": o.Dir, "filename": s.Name.String(), "view": s.Table, "err": wcsv.Error(), }).Error(asset.Tr("error_file_csv_view_to")) } }
func (o OsFile) GetAll() []string { var out []string files, err := ioutil.ReadDir(o.Dir) if err != nil { log.WithFields(log.Fields{ "err": err, "dir": o.Dir, }).Error(asset.Tr("error_dir_read")) } for _, file := range files { if !strings.HasSuffix(file.Name(), extension) { continue } out = append(out, strings.TrimSuffix(file.Name(), extension)) } return out }
func main() { prodValHeader = []string{asset.Tr("csv_header_item_product"), asset.Tr("csv_header_item_value", 1), asset.Tr("csv_header_item_unit", 1), asset.Tr("csv_header_item_value", 2), asset.Tr("csv_header_item_unit", 2), asset.Tr("csv_header_item_value", 3), asset.Tr("csv_header_item_unit", 3), asset.Tr("csv_header_item_value", 4), asset.Tr("csv_header_item_unit", 4), } // LOGGING logfile := loggingPrefix + asset.Tr("file_name_log") f, err1 := os.OpenFile(logfile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err1 != nil { log.WithFields(log.Fields{ "filename": logfile, "err": err1, }).Error(asset.Tr("error_file_open")) } defer f.Close() log.SetOutput(f) log.SetFormatter(&log.JSONFormatter{}) log.SetLevel(log.ErrorLevel) // UPDATE all := sheet.AllSheets(files) for _, name := range all { s := sheet.NewSheet(name, files) its := items.FromStringTable(s.Table.GetContentWithRowHeader()) var cmd skelet.Ider switch s.Name.Act { case asset.Tr("file_name_stock_in"): cmd = stock.InCmd{stockId, its, s.Name.Time()} case asset.Tr("file_name_stock_out"): cmd = stock.OutCmd{stockId, its, s.Name.Time()} case asset.Tr("file_name_inventory"): cmd = stock.InventoryCmd{stockId, its, s.Name.Time()} default: log.Error(asset.Tr("no_action_for_filename_error")) } skelet.ExecuteCommand(skelet.Cmd{T: cmd, Route: stockRoute}, stockCmd.Chain) } // POPULATE VIEW stockInt, err4 := endPt.Db.Get("main") iStock := stockInt.(*stock.Stock).Items if err4 != nil { log.WithFields(log.Fields{ "err": err4, }).Error(asset.Tr("error_query_stock")) } prodValRender := func(tab *strtab.T) [][]string { return tab.GetContentWithHeaders(false) } prodEvolRender := func(tab *strtab.T) [][]string { return tab.GetContentWithHeaders(true) } sheet.Sheet{ sheet.NewBasicFilename(asset.Tr("file_name_stock")), strtab.NewT(prodValHeader, iStock.StringSlice()...).Sort(), prodValRender}.Put(files) sheet.Sheet{ sheet.NewDraftFilename(3, asset.Tr("file_name_inventory")), strtab.NewT(prodValHeader, iStock.StringSlice()...).Sort(), prodValRender}.Put(files) sheet.Sheet{ sheet.NewDraftFilename(1, asset.Tr("file_name_stock_in")), strtab.NewT(prodValHeader, iStock.Empty().StringSlice()...).Sort(), prodValRender}.Put(files) sheet.Sheet{ sheet.NewDraftFilename(2, asset.Tr("file_name_stock_out")), strtab.NewT(prodValHeader, iStock.Empty().StringSlice()...).Sort(), prodValRender}.Put(files) sheet.Sheet{ sheet.NewBasicFilename(asset.Tr("file_name_product")), strtab.NewTfromMap(items.ItemsMapToStringMapTable( endPt.ProdValEvol("main"))).Sort().Transpose().Sort(), prodEvolRender}.Put(files) }
func NewDraftFilename(id int, act string) Filename { return Filename{asset.Tr("file_prefix_draft"), time.Now(), strconv.Itoa(id), act, ""} }