Exemplo n.º 1
0
func (h *Handler) handleUpdate(w http.ResponseWriter, r *http.Request) {
	_, err := session.Parse(r)
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	path := config.Get("invoice_data")
	v, err := invoice.Load(path)
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	key := r.FormValue("key")
	hours, _ := strconv.Atoi(r.FormValue("hours"))

	v.SetHours(key, hours)
	err = v.Save(path)
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	url := r.FormValue("url")
	if url == "" {
		url = config.Get("baseurl")
	}

	http.Redirect(w, r, url, http.StatusFound)
}
Exemplo n.º 2
0
func (h *Handler) handleHome(w http.ResponseWriter, r *http.Request) {
	s, _ := session.Parse(r)
	if s != nil {
		s.Save(w, true)
	}

	v, err := invoice.Load(config.Get("invoice_data"))
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	urlpath := strings.Trim(r.URL.Path, "/")
	segments := strings.Split(urlpath, "/")

	if len(segments) == 2 {
		v.SetSelected(segments[1])
	}

	msg, _ := flashdata.Get(w, r)

	m := map[string]interface{}{
		"Hours":    make([]int, 25),
		"Invoice":  v,
		"LoggedIn": s != nil,
		"Message":  msg,
		"Url":      r.URL.Path,
	}

	h.Templates.ExecuteTemplate(w, "home.html", m)
}
Exemplo n.º 3
0
func (h *Handler) handleClose(w http.ResponseWriter, r *http.Request) {
	_, err := session.Parse(r)
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	path := config.Get("invoice_data")
	v, err := invoice.Load(path)
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	// HTML
	htmlName := fmt.Sprintf("%s-%s.html", v.User.Prefix,
		v.Invoice.Entries[v.Invoice.EndDate].Key)
	htmlPath := filepath.Join(config.Get("archive_path"), htmlName)

	var htmlBuf bytes.Buffer
	err = h.Templates.ExecuteTemplate(&htmlBuf, "invoice.html", v)
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	err = ioutil.WriteFile(htmlPath, htmlBuf.Bytes(), 0644)
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	// JSON
	jsonName := fmt.Sprintf("%s-%s.txt", v.User.Prefix,
		v.Invoice.Entries[v.Invoice.EndDate].Key)
	jsonPath := filepath.Join(config.Get("archive_path"), jsonName)

	jsonBytes, err := json.MarshalIndent(v, "", "    ")
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	err = ioutil.WriteFile(jsonPath, jsonBytes, 0644)
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	// EMAIL
	m := awsses.New(
		config.Get("awsses_sender"),
		config.Get("awsses_sender"),
		fmt.Sprintf(
			"High Tech Timesheet %s/%s/%d",
			v.Invoice.Entries[v.Invoice.EndDate].MM,
			v.Invoice.Entries[v.Invoice.EndDate].DD,
			v.Invoice.Entries[v.Invoice.EndDate].YYYY),
		"",
		fmt.Sprintf("%d Hours", v.Invoice.Total),
		&awsses.MessageAttachment{htmlBuf.Bytes(), "text/html", htmlName},
		&awsses.MessageAttachment{jsonBytes, "text/plain", jsonName})

	err = m.Send(
		config.Get("awsses_baseurl"),
		config.Get("awsses_accesskey"),
		config.Get("awsses_secretkey"))
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	v2, err := invoice.New(v.Invoice.Entries[v.Invoice.EndDate].Key, time.Hour*24)
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	v2.User = v.User
	v2.User.LastInvoice = fmt.Sprintf("%s/%s",
		config.Get("archive_baseurl"), htmlName)

	err = v2.Save(path)
	if err != nil {
		logger.Error(w, err)
		h.serveServerError(w, r)
		return
	}

	http.Redirect(w, r, config.Get("baseurl"), http.StatusFound)
}