|
|
|
@ -39,7 +39,7 @@ func IndexInvoices(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mustCollectInvoiceEntries(ctx context.Context, conn *Conn, company *Company, locale *Locale) []*InvoiceEntry {
|
|
|
|
|
rows := conn.MustQuery(ctx, "select invoice.slug, invoice_date, invoice_number, contact.business_name, contact.slug, invoice.invoice_status, isi18n.name, to_price(total, decimal_digits) from invoice join contact using (contact_id) join invoice_status_i18n isi18n on invoice.invoice_status = isi18n.invoice_status and isi18n.lang_tag = $2 join invoice_amount using (invoice_id) join currency using (currency_code) where invoice.company_id = $1 order by invoice_date, invoice_number", company.Id, locale.Language.String())
|
|
|
|
|
rows := conn.MustQuery(ctx, "select invoice.slug, invoice_date, invoice_number, contact.business_name, contact.slug, invoice.invoice_status, isi18n.name, to_price(total, decimal_digits) from invoice join contact using (contact_id) join invoice_status_i18n isi18n on invoice.invoice_status = isi18n.invoice_status and isi18n.lang_tag = $2 join invoice_amount using (invoice_id) join currency using (currency_code) where invoice.company_id = $1 order by invoice_date desc, invoice_number desc", company.Id, locale.Language.String())
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
var entries []*InvoiceEntry
|
|
|
|
@ -92,12 +92,12 @@ func ServeInvoice(w http.ResponseWriter, r *http.Request, params httprouter.Para
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
defer stdout.Close()
|
|
|
|
|
defer mustClose(stdout)
|
|
|
|
|
if err = cmd.Start(); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
go func() {
|
|
|
|
|
defer stdin.Close()
|
|
|
|
|
defer mustClose(stdin)
|
|
|
|
|
mustRenderAppTemplate(stdin, r, "invoices/view.gohtml", invoice)
|
|
|
|
|
}()
|
|
|
|
|
w.Header().Set("Content-Type", "application/pdf")
|
|
|
|
@ -113,6 +113,12 @@ func ServeInvoice(w http.ResponseWriter, r *http.Request, params httprouter.Para
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mustClose(closer io.Closer) {
|
|
|
|
|
if err := closer.Close(); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type invoice struct {
|
|
|
|
|
Number string
|
|
|
|
|
Slug string
|
|
|
|
@ -256,28 +262,16 @@ func HandleAddInvoice(w http.ResponseWriter, r *http.Request, _ httprouter.Param
|
|
|
|
|
http.Error(w, err.Error(), http.StatusForbidden)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
switch r.Form.Get("action") {
|
|
|
|
|
case "update":
|
|
|
|
|
form.Update()
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
if !form.Validate() {
|
|
|
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
|
|
|
mustRenderNewInvoiceForm(w, r, form)
|
|
|
|
|
case "products":
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
mustRenderNewInvoiceProductsForm(w, r, form)
|
|
|
|
|
case "add":
|
|
|
|
|
if !form.Validate() {
|
|
|
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
|
|
|
mustRenderNewInvoiceForm(w, r, form)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
conn.MustExec(r.Context(), "select add_invoice($1, $2, $3, $4, $5, $6)", company.Id, form.Number, form.Date, form.Customer, form.Notes, NewInvoiceProductArray(form.Products))
|
|
|
|
|
http.Redirect(w, r, companyURI(company, "/invoices"), http.StatusSeeOther)
|
|
|
|
|
default:
|
|
|
|
|
http.Error(w, gettext("Invalid action", locale), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
slug := conn.MustGetText(r.Context(), "", "select add_invoice($1, $2, $3, $4, $5, $6)", company.Id, form.Number, form.Date, form.Customer, form.Notes, NewInvoiceProductArray(form.Products))
|
|
|
|
|
http.Redirect(w, r, companyURI(company, "/invoices/"+slug), http.StatusSeeOther)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func HandleAddProductsToInvoice(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
|
func HandleNewInvoiceAction(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
|
|
|
|
|
locale := getLocale(r)
|
|
|
|
|
conn := getConn(r)
|
|
|
|
|
company := mustGetCompany(r)
|
|
|
|
@ -286,25 +280,25 @@ func HandleAddProductsToInvoice(w http.ResponseWriter, r *http.Request, _ httpro
|
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
index := len(form.Products)
|
|
|
|
|
productsId := r.Form["id"]
|
|
|
|
|
rows := conn.MustQuery(r.Context(), "select product_id, name, description, to_price(price, decimal_digits), 1 as quantity, 0 as discount, array_remove(array_agg(tax_id), null) from product join company using (company_id) join currency using (currency_code) left join product_tax using (product_id) where product_id = any ($1) group by product_id, name, description, price, decimal_digits", productsId)
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
product := newInvoiceProductForm(index, company, locale, form.Tax.Options)
|
|
|
|
|
if err := rows.Scan(product.ProductId, product.Name, product.Description, product.Price, product.Quantity, product.Discount, product.Tax); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
form.Products = append(form.Products, product)
|
|
|
|
|
index++
|
|
|
|
|
if err := verifyCsrfTokenValid(r); err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusForbidden)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if rows.Err() != nil {
|
|
|
|
|
panic(rows.Err())
|
|
|
|
|
switch r.Form.Get("action") {
|
|
|
|
|
case "update":
|
|
|
|
|
form.Update()
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
mustRenderNewInvoiceForm(w, r, form)
|
|
|
|
|
case "select-products":
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
mustRenderNewInvoiceProductsForm(w, r, form)
|
|
|
|
|
case "add-products":
|
|
|
|
|
form.AddProducts(r.Context(), conn, r.Form["id"])
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
mustRenderNewInvoiceForm(w, r, form)
|
|
|
|
|
default:
|
|
|
|
|
http.Error(w, gettext("Invalid action", locale), http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
mustRenderNewInvoiceForm(w, r, form)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type invoiceForm struct {
|
|
|
|
@ -406,6 +400,23 @@ func (form *invoiceForm) Update() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (form *invoiceForm) AddProducts(ctx context.Context, conn *Conn, productsId []string) {
|
|
|
|
|
index := len(form.Products)
|
|
|
|
|
rows := conn.MustQuery(ctx, "select product_id, name, description, to_price(price, decimal_digits), 1 as quantity, 0 as discount, array_remove(array_agg(tax_id), null) from product join company using (company_id) join currency using (currency_code) left join product_tax using (product_id) where product_id = any ($1) group by product_id, name, description, price, decimal_digits", productsId)
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
product := newInvoiceProductForm(index, form.company, form.locale, form.Tax.Options)
|
|
|
|
|
if err := rows.Scan(product.ProductId, product.Name, product.Description, product.Price, product.Quantity, product.Discount, product.Tax); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
form.Products = append(form.Products, product)
|
|
|
|
|
index++
|
|
|
|
|
}
|
|
|
|
|
if rows.Err() != nil {
|
|
|
|
|
panic(rows.Err())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mustGetTaxOptions(ctx context.Context, conn *Conn, company *Company) []*SelectOption {
|
|
|
|
|
return MustGetOptions(ctx, conn, "select tax_id::text, name from tax where company_id = $1 order by name", company.Id)
|
|
|
|
|
}
|
|
|
|
|