Call add_invoice when adding invoice from Go
I needed to add binary encoding function for the new_invoice_product type, and currently it is not correct: i hardcoded the OID values, but they are going to be different on a different database.
This commit is contained in:
parent
697f821310
commit
98ac17a129
1
go.mod
1
go.mod
|
@ -10,6 +10,7 @@ require (
|
|||
)
|
||||
|
||||
require (
|
||||
github.com/jackc/pgio v1.0.0
|
||||
github.com/jackc/pgtype v1.12.0
|
||||
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90 // indirect
|
||||
)
|
||||
|
|
63
pkg/db.go
63
pkg/db.go
|
@ -4,6 +4,8 @@ import (
|
|||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/jackc/pgio"
|
||||
"github.com/jackc/pgtype"
|
||||
"github.com/jackc/pgx/v4"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
)
|
||||
|
@ -12,6 +14,66 @@ type Db struct {
|
|||
*pgxpool.Pool
|
||||
}
|
||||
|
||||
type newInvoiceProductArray []*invoiceProductForm
|
||||
|
||||
type discountRate struct {
|
||||
pgtype.Numeric
|
||||
}
|
||||
|
||||
func (form *invoiceProductForm) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) (newBuff []byte, err error) {
|
||||
productId := &pgtype.Int4{}
|
||||
if err = productId.Set(form.ProductId.Val); err != nil {
|
||||
return
|
||||
}
|
||||
name := &pgtype.Text{String: form.Name.Val, Status: pgtype.Present}
|
||||
description := &pgtype.Text{String: form.Description.Val, Status: pgtype.Present}
|
||||
price := &pgtype.Text{String: form.Price.Val, Status: pgtype.Present}
|
||||
quantity := &pgtype.Int4{}
|
||||
if err = quantity.Set(form.Quantity.Val); err != nil {
|
||||
return
|
||||
}
|
||||
discount := &discountRate{}
|
||||
if err = discount.Set(form.Discount.Val); err != nil {
|
||||
return
|
||||
}
|
||||
tax := &pgtype.Int4Array{}
|
||||
if err = tax.Set(form.Tax.Selected); err != nil {
|
||||
return
|
||||
}
|
||||
ct := pgtype.CompositeFields{productId, name, description, price, quantity, discount, tax}
|
||||
return ct.EncodeBinary(ci, buf)
|
||||
}
|
||||
|
||||
func (src newInvoiceProductArray) EncodeBinary(ci *pgtype.ConnInfo, buf []byte) ([]byte, error) {
|
||||
arrayHeader := pgtype.ArrayHeader{
|
||||
Dimensions: []pgtype.ArrayDimension{{Length: int32(len(src)), LowerBound: 1}},
|
||||
}
|
||||
|
||||
/*typeName := "numerus.new_invoice_product"
|
||||
if dt, ok := ci.DataTypeForName(typeName); ok {
|
||||
arrayHeader.ElementOID = int32(dt.OID)
|
||||
} else {
|
||||
return nil, fmt.Errorf("unable to find oid for type name %v", typeName)
|
||||
}*/
|
||||
arrayHeader.ElementOID = 69053
|
||||
buf = arrayHeader.EncodeBinary(ci, buf)
|
||||
for i := range src {
|
||||
sp := len(buf)
|
||||
buf = pgio.AppendInt32(buf, -1)
|
||||
|
||||
elemBuf, err := src[i].EncodeBinary(ci, buf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if elemBuf != nil {
|
||||
buf = elemBuf
|
||||
pgio.SetInt32(buf[sp:], int32(len(buf[sp:])-4))
|
||||
}
|
||||
}
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func NewDatabase(ctx context.Context, connString string) (*Db, error) {
|
||||
config, err := pgxpool.ParseConfig(connString)
|
||||
if err != nil {
|
||||
|
@ -20,6 +82,7 @@ func NewDatabase(ctx context.Context, connString string) (*Db, error) {
|
|||
|
||||
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
|
||||
_, err := conn.Exec(context.Background(), "SET search_path TO numerus, public")
|
||||
conn.ConnInfo().RegisterDataType(pgtype.DataType{Value: &discountRate{}, Name: "discount_rate", OID: 69002})
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ package pkg
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/jackc/pgx/v4"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"html/template"
|
||||
"math"
|
||||
|
@ -139,22 +138,7 @@ func HandleAddInvoice(w http.ResponseWriter, r *http.Request, _ httprouter.Param
|
|||
mustRenderNewInvoiceForm(w, r, form)
|
||||
return
|
||||
}
|
||||
tx := conn.MustBegin(r.Context())
|
||||
invoiceId := tx.MustGetInteger(r.Context(), "insert into invoice (company_id, invoice_number, invoice_date, contact_id, notes, currency_code) select company_id, $2, $3, $4, $5, currency_code from company join currency using (currency_code) where company_id = $1 returning invoice_id", company.Id, form.Number, form.Date, form.Customer, form.Notes)
|
||||
batch := &pgx.Batch{}
|
||||
for _, product := range form.Products {
|
||||
batch.Queue("insert into invoice_product(invoice_id, product_id, name, description, price, quantity, discount_rate) select $2, $3, $4, $5, parse_price($6, decimal_digits), $7, $8 / 100::decimal from company join currency using (currency_code) where company_id = $1", company.Id, invoiceId, product.ProductId, product.Name, product.Description, product.Price, product.Quantity.Integer(), product.Discount.Integer())
|
||||
}
|
||||
br := tx.SendBatch(r.Context(), batch)
|
||||
for range form.Products {
|
||||
if _, err := br.Exec(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
if err := br.Close(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
tx.MustCommit(r.Context())
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue