2023-06-07 14:35:31 +00:00
package pkg
import (
"archive/zip"
"bytes"
"context"
"errors"
"fmt"
"github.com/julienschmidt/httprouter"
"html/template"
"io"
"log"
"math"
"net/http"
"os"
"os/exec"
"sort"
"strconv"
"strings"
"time"
)
type QuoteEntry struct {
Slug string
Date time . Time
Number string
Total string
CustomerName string
Tags [ ] string
Status string
StatusLabel string
}
type QuotesIndexPage struct {
Quotes [ ] * QuoteEntry
2023-06-20 09:33:28 +00:00
TotalAmount string
2023-06-07 14:35:31 +00:00
Filters * quoteFilterForm
QuoteStatuses map [ string ] string
}
func IndexQuotes ( w http . ResponseWriter , r * http . Request , _ httprouter . Params ) {
conn := getConn ( r )
locale := getLocale ( r )
company := mustGetCompany ( r )
filters := newQuoteFilterForm ( r . Context ( ) , conn , locale , company )
if err := filters . Parse ( r ) ; err != nil {
http . Error ( w , err . Error ( ) , http . StatusBadRequest )
return
}
page := & QuotesIndexPage {
2023-06-20 09:33:28 +00:00
Quotes : mustCollectQuoteEntries ( r . Context ( ) , conn , locale , filters ) ,
TotalAmount : mustComputeQuotesTotalAmount ( r . Context ( ) , conn , filters ) ,
2023-06-07 14:35:31 +00:00
Filters : filters ,
QuoteStatuses : mustCollectQuoteStatuses ( r . Context ( ) , conn , locale ) ,
}
mustRenderMainTemplate ( w , r , "quotes/index.gohtml" , page )
}
2023-06-20 09:33:28 +00:00
func mustCollectQuoteEntries ( ctx context . Context , conn * Conn , locale * Locale , filters * quoteFilterForm ) [ ] * QuoteEntry {
where , args := filters . BuildQuery ( [ ] interface { } { locale . Language . String ( ) } )
2023-06-07 14:35:31 +00:00
rows := conn . MustQuery ( ctx , fmt . Sprintf ( `
select quote . slug
, quote_date
, quote_number
Split contact relation into tax_details, phone, web, and email
We need to have contacts with just a name: we need to assign
freelancer’s quote as expense linked the government, but of course we
do not have a phone or email for that “contact”, much less a VATIN or
other tax details.
It is also interesting for other expenses-only contacts to not have to
input all tax details, as we may not need to invoice then, thus are
useless for us, but sometimes it might be interesting to have them,
“just in case”.
Of course, i did not want to make nullable any of the tax details
required to generate an invoice, otherwise we could allow illegal
invoices. Therefore, that data had to go in a different relation,
and invoice’s foreign key update to point to that relation, not just
customer, or we would again be able to create invalid invoices.
We replaced the contact’s trade name with just name, because we do not
need _three_ names for a contact, but we _do_ need two: the one we use
to refer to them and the business name for tax purposes.
The new contact_phone, contact_web, and contact_email relations could be
simply a nullable field, but i did not see the point, since there are
not that many instances where i need any of this data.
Now company.taxDetailsForm is no longer “the same as contactForm with
some extra fields”, because i have to add a check whether the user needs
to invoice the contact, to check that the required values are there.
I have an additional problem with the contact form when not using
JavaScript: i must set the required field to all tax details fields to
avoid the “(optional)” suffix, and because they _are_ required when
that checkbox is enabled, but i can not set them optional when the check
is unchecked. My solution for now is to ignore the form validation,
and later i will add some JavaScript that adds the validation again,
so it will work in all cases.
2023-06-30 19:32:48 +00:00
, coalesce ( contact . name , ' ' )
2023-06-07 14:35:31 +00:00
, quote . tags
, quote . quote_status
, isi18n . name
, to_price ( total , decimal_digits )
from quote
left join quote_contact using ( quote_id )
left join contact using ( contact_id )
join quote_status_i18n isi18n on quote . quote_status = isi18n . quote_status and isi18n . lang_tag = $ 1
join quote_amount using ( quote_id )
join currency using ( currency_code )
where ( % s )
order by quote_date desc
, quote_number desc
2023-06-20 09:33:28 +00:00
` , where ) , args ... )
2023-06-07 14:35:31 +00:00
defer rows . Close ( )
var entries [ ] * QuoteEntry
for rows . Next ( ) {
entry := & QuoteEntry { }
if err := rows . Scan ( & entry . Slug , & entry . Date , & entry . Number , & entry . CustomerName , & entry . Tags , & entry . Status , & entry . StatusLabel , & entry . Total ) ; err != nil {
panic ( err )
}
entries = append ( entries , entry )
}
if rows . Err ( ) != nil {
panic ( rows . Err ( ) )
}
return entries
}
2023-06-20 09:33:28 +00:00
func mustComputeQuotesTotalAmount ( ctx context . Context , conn * Conn , filters * quoteFilterForm ) string {
where , args := filters . BuildQuery ( nil )
return conn . MustGetText ( ctx , "0" , fmt . Sprintf ( `
select to_price ( sum ( total ) : : integer , decimal_digits )
from quote
left join quote_contact using ( quote_id )
join quote_amount using ( quote_id )
join currency using ( currency_code )
where ( % s )
group by decimal_digits
` , where ) , args ... )
}
2023-06-07 14:35:31 +00:00
func mustCollectQuoteStatuses ( ctx context . Context , conn * Conn , locale * Locale ) map [ string ] string {
rows := conn . MustQuery ( ctx , "select quote_status.quote_status, isi18n.name from quote_status join quote_status_i18n isi18n using(quote_status) where isi18n.lang_tag = $1 order by quote_status" , locale . Language . String ( ) )
defer rows . Close ( )
statuses := map [ string ] string { }
for rows . Next ( ) {
var key , name string
if err := rows . Scan ( & key , & name ) ; err != nil {
panic ( err )
}
statuses [ key ] = name
}
if rows . Err ( ) != nil {
panic ( rows . Err ( ) )
}
return statuses
}
type quoteFilterForm struct {
locale * Locale
company * Company
Customer * SelectField
QuoteStatus * SelectField
QuoteNumber * InputField
FromDate * InputField
ToDate * InputField
Tags * TagsField
TagsCondition * ToggleField
}
func newQuoteFilterForm ( ctx context . Context , conn * Conn , locale * Locale , company * Company ) * quoteFilterForm {
return & quoteFilterForm {
locale : locale ,
company : company ,
Customer : & SelectField {
Name : "customer" ,
2023-06-20 09:37:02 +00:00
Label : pgettext ( "input" , "Customer" , locale ) ,
2023-06-07 14:35:31 +00:00
EmptyLabel : gettext ( "All customers" , locale ) ,
Options : mustGetContactOptions ( ctx , conn , company ) ,
} ,
QuoteStatus : & SelectField {
Name : "quote_status" ,
Label : pgettext ( "input" , "Quotation Status" , locale ) ,
EmptyLabel : gettext ( "All status" , locale ) ,
Options : MustGetOptions ( ctx , conn , "select quote_status.quote_status, isi18n.name from quote_status join quote_status_i18n isi18n using(quote_status) where isi18n.lang_tag = $1 order by quote_status" , locale . Language . String ( ) ) ,
} ,
QuoteNumber : & InputField {
Name : "number" ,
Label : pgettext ( "input" , "Quotation Number" , locale ) ,
Type : "search" ,
} ,
FromDate : & InputField {
Name : "from_date" ,
Label : pgettext ( "input" , "From Date" , locale ) ,
Type : "date" ,
} ,
ToDate : & InputField {
Name : "to_date" ,
Label : pgettext ( "input" , "To Date" , locale ) ,
Type : "date" ,
} ,
Tags : & TagsField {
Name : "tags" ,
Label : pgettext ( "input" , "Tags" , locale ) ,
} ,
TagsCondition : & ToggleField {
Name : "tags_condition" ,
Label : pgettext ( "input" , "Tags Condition" , locale ) ,
Selected : "and" ,
FirstOption : & ToggleOption {
Value : "and" ,
Label : pgettext ( "tag condition" , "All" , locale ) ,
Description : gettext ( "Quotations must have all the specified labels." , locale ) ,
} ,
SecondOption : & ToggleOption {
Value : "or" ,
Label : pgettext ( "tag condition" , "Any" , locale ) ,
Description : gettext ( "Quotations must have at least one of the specified labels." , locale ) ,
} ,
} ,
}
}
func ( form * quoteFilterForm ) Parse ( r * http . Request ) error {
if err := r . ParseForm ( ) ; err != nil {
return err
}
form . Customer . FillValue ( r )
form . QuoteStatus . FillValue ( r )
form . QuoteNumber . FillValue ( r )
form . FromDate . FillValue ( r )
form . ToDate . FillValue ( r )
form . Tags . FillValue ( r )
form . TagsCondition . FillValue ( r )
return nil
}
2023-07-16 18:56:11 +00:00
func ( form * quoteFilterForm ) HasValue ( ) bool {
return form . Customer . HasValue ( ) ||
form . QuoteStatus . HasValue ( ) ||
form . QuoteNumber . HasValue ( ) ||
form . FromDate . HasValue ( ) ||
form . ToDate . HasValue ( ) ||
form . Tags . HasValue ( )
}
2023-06-20 09:33:28 +00:00
func ( form * quoteFilterForm ) BuildQuery ( args [ ] interface { } ) ( string , [ ] interface { } ) {
var where [ ] string
appendWhere := func ( expression string , value interface { } ) {
args = append ( args , value )
where = append ( where , fmt . Sprintf ( expression , len ( args ) ) )
}
maybeAppendWhere := func ( expression string , value string , conv func ( string ) interface { } ) {
if value != "" {
if conv == nil {
appendWhere ( expression , value )
} else {
appendWhere ( expression , conv ( value ) )
}
}
}
appendWhere ( "quote.company_id = $%d" , form . company . Id )
maybeAppendWhere ( "contact_id = $%d" , form . Customer . String ( ) , func ( v string ) interface { } {
customerId , _ := strconv . Atoi ( form . Customer . Selected [ 0 ] )
return customerId
} )
maybeAppendWhere ( "quote.quote_status = $%d" , form . QuoteStatus . String ( ) , nil )
maybeAppendWhere ( "quote_number = $%d" , form . QuoteNumber . String ( ) , nil )
maybeAppendWhere ( "quote_date >= $%d" , form . FromDate . String ( ) , nil )
maybeAppendWhere ( "quote_date <= $%d" , form . ToDate . String ( ) , nil )
if len ( form . Tags . Tags ) > 0 {
if form . TagsCondition . Selected == "and" {
appendWhere ( "quote.tags @> $%d" , form . Tags )
} else {
appendWhere ( "quote.tags && $%d" , form . Tags )
}
}
return strings . Join ( where , ") AND (" ) , args
}
2023-06-07 14:35:31 +00:00
func ServeQuote ( w http . ResponseWriter , r * http . Request , params httprouter . Params ) {
conn := getConn ( r )
company := mustGetCompany ( r )
slug := params [ 0 ] . Value
switch slug {
case "new" :
locale := getLocale ( r )
form := newQuoteForm ( r . Context ( ) , conn , locale , company )
Return HTTP 404 instead of 500 for invalid UUID values in URL
Since most of PL/pgSQL functions accept a `uuid` domain, we get an error
if the value is not valid, forcing us to return an HTTP 500, as we
can not detect that the error was due to that.
Instead, i now validate that the slug is indeed a valid UUID before
attempting to send it to the database, returning the correct HTTP error
code and avoiding useless calls to the database.
I based the validation function of Parse() from Google’s uuid package[0]
because this function is an order or magnitude faster in benchmarks:
goos: linux
goarch: amd64
pkg: dev.tandem.ws/tandem/numerus/pkg
cpu: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
BenchmarkValidUuid-4 36946050 29.37 ns/op
BenchmarkValidUuid_Re-4 3633169 306.70 ns/op
The regular expression used for the benchmark was:
var re = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
And the input parameter for both functions was the following valid UUID,
because most of the time the passed UUID will be valid:
"f47ac10b-58cc-0372-8567-0e02b2c3d479"
I did not use the uuid package, even though it is in Debian’s
repository, because i only need to check whether the value is valid,
not convert it to a byte array. As far as i know, that package can not
do that.
[0]: https://github.com/google/uuid
2023-07-17 09:46:11 +00:00
if quoteToDuplicate := r . URL . Query ( ) . Get ( "duplicate" ) ; ValidUuid ( quoteToDuplicate ) {
2023-06-07 14:35:31 +00:00
form . MustFillFromDatabase ( r . Context ( ) , conn , quoteToDuplicate )
form . QuoteStatus . Selected = [ ] string { "created" }
}
form . Date . Val = time . Now ( ) . Format ( "2006-01-02" )
w . WriteHeader ( http . StatusOK )
mustRenderNewQuoteForm ( w , r , form )
case "product-form" :
query := r . URL . Query ( )
index , _ := strconv . Atoi ( query . Get ( "index" ) )
form := newQuoteProductForm ( index , company , getLocale ( r ) , mustGetTaxOptions ( r . Context ( ) , conn , company ) )
productSlug := query . Get ( "slug" )
if len ( productSlug ) > 0 {
if ! form . MustFillFromDatabase ( r . Context ( ) , conn , productSlug ) {
http . NotFound ( w , r )
return
}
quantity , _ := strconv . Atoi ( query . Get ( "product.quantity." + strconv . Itoa ( index ) ) )
if quantity > 0 {
form . Quantity . Val = strconv . Itoa ( quantity )
}
w . Header ( ) . Set ( HxTriggerAfterSettle , "recompute" )
}
mustRenderStandaloneTemplate ( w , r , "quotes/product-form.gohtml" , form )
default :
pdf := false
if strings . HasSuffix ( slug , ".pdf" ) {
pdf = true
slug = slug [ : len ( slug ) - len ( ".pdf" ) ]
}
Return HTTP 404 instead of 500 for invalid UUID values in URL
Since most of PL/pgSQL functions accept a `uuid` domain, we get an error
if the value is not valid, forcing us to return an HTTP 500, as we
can not detect that the error was due to that.
Instead, i now validate that the slug is indeed a valid UUID before
attempting to send it to the database, returning the correct HTTP error
code and avoiding useless calls to the database.
I based the validation function of Parse() from Google’s uuid package[0]
because this function is an order or magnitude faster in benchmarks:
goos: linux
goarch: amd64
pkg: dev.tandem.ws/tandem/numerus/pkg
cpu: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
BenchmarkValidUuid-4 36946050 29.37 ns/op
BenchmarkValidUuid_Re-4 3633169 306.70 ns/op
The regular expression used for the benchmark was:
var re = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
And the input parameter for both functions was the following valid UUID,
because most of the time the passed UUID will be valid:
"f47ac10b-58cc-0372-8567-0e02b2c3d479"
I did not use the uuid package, even though it is in Debian’s
repository, because i only need to check whether the value is valid,
not convert it to a byte array. As far as i know, that package can not
do that.
[0]: https://github.com/google/uuid
2023-07-17 09:46:11 +00:00
if ! ValidUuid ( slug ) {
http . NotFound ( w , r )
return
}
2023-06-07 14:35:31 +00:00
quo := mustGetQuote ( r . Context ( ) , conn , company , slug )
if quo == nil {
http . NotFound ( w , r )
return
}
if pdf {
w . Header ( ) . Set ( "Content-Type" , "application/pdf" )
mustWriteQuotePdf ( w , r , quo )
} else {
mustRenderMainTemplate ( w , r , "quotes/view.gohtml" , quo )
}
}
}
func mustWriteQuotePdf ( w io . Writer , r * http . Request , quo * quote ) {
2023-06-15 21:16:53 +00:00
cmd := exec . Command ( "weasyprint" , "--stylesheet" , "web/static/invoice.css" , "-" , "-" )
2023-06-07 14:35:31 +00:00
var stderr bytes . Buffer
cmd . Stderr = & stderr
stdin , err := cmd . StdinPipe ( )
if err != nil {
panic ( err )
}
stdout , err := cmd . StdoutPipe ( )
if err != nil {
panic ( err )
}
defer func ( ) {
err := stdout . Close ( )
if ! errors . Is ( err , os . ErrClosed ) {
panic ( err )
}
} ( )
if err = cmd . Start ( ) ; err != nil {
panic ( err )
}
go func ( ) {
defer mustClose ( stdin )
mustRenderAppTemplate ( stdin , r , "quotes/view.gohtml" , quo )
} ( )
if _ , err = io . Copy ( w , stdout ) ; err != nil {
panic ( err )
}
if err := cmd . Wait ( ) ; err != nil {
log . Printf ( "ERR - %v\n" , stderr . String ( ) )
panic ( err )
}
}
type quote struct {
Number string
Slug string
Date time . Time
Quoter taxDetails
Allow empty contact and payment method for quotes
I have to use a value to be used as “none” for payment method and
contact. In PL/pgSQL add_quote and edit_quote functions, that value is
NULL, while in forms it is the empty string. I can not simply pass the
empty string for either of these fields because PL/pgSQL expects
(nullable) integers, and "" is not a valid integer and is not NULL
either. A conversion is necessary.
Apparently, Go’s nil is not a valid representation for SQL’s NULL with
pgx, and had to use sql.NullString instead.
I also needed to coalesce contact’s VATIN and phone, because null values
can not be scanned to *string. I did not do that before because
`coalesce(vatin, '')` throws an error that '' is not a valid VATIN and
just left as is, wrongly expecting that pgx would do the job of leaving
the string blank for me. It does not.
Lastly, i can not blindly write Quotee’s tax details in the quote’s view
page, or we would see the (), characters for the empty address info.
2023-06-08 11:05:41 +00:00
HasQuotee bool
Split contact relation into tax_details, phone, web, and email
We need to have contacts with just a name: we need to assign
freelancer’s quote as expense linked the government, but of course we
do not have a phone or email for that “contact”, much less a VATIN or
other tax details.
It is also interesting for other expenses-only contacts to not have to
input all tax details, as we may not need to invoice then, thus are
useless for us, but sometimes it might be interesting to have them,
“just in case”.
Of course, i did not want to make nullable any of the tax details
required to generate an invoice, otherwise we could allow illegal
invoices. Therefore, that data had to go in a different relation,
and invoice’s foreign key update to point to that relation, not just
customer, or we would again be able to create invalid invoices.
We replaced the contact’s trade name with just name, because we do not
need _three_ names for a contact, but we _do_ need two: the one we use
to refer to them and the business name for tax purposes.
The new contact_phone, contact_web, and contact_email relations could be
simply a nullable field, but i did not see the point, since there are
not that many instances where i need any of this data.
Now company.taxDetailsForm is no longer “the same as contactForm with
some extra fields”, because i have to add a check whether the user needs
to invoice the contact, to check that the required values are there.
I have an additional problem with the contact form when not using
JavaScript: i must set the required field to all tax details fields to
avoid the “(optional)” suffix, and because they _are_ required when
that checkbox is enabled, but i can not set them optional when the check
is unchecked. My solution for now is to ignore the form validation,
and later i will add some JavaScript that adds the validation again,
so it will work in all cases.
2023-06-30 19:32:48 +00:00
HasTaxDetails bool
2023-06-07 14:35:31 +00:00
Quotee taxDetails
TermsAndConditions string
Notes string
PaymentInstructions string
Products [ ] * quoteProduct
Subtotal string
Taxes [ ] [ ] string
TaxClasses [ ] string
HasDiscounts bool
Total string
LegalDisclaimer string
}
type quoteProduct struct {
Name string
Description string
Price string
Discount int
Quantity int
Taxes map [ string ] int
Subtotal string
Total string
}
func mustGetQuote ( ctx context . Context , conn * Conn , company * Company , slug string ) * quote {
quo := & quote {
Slug : slug ,
}
var quoteId int
var decimalDigits int
if notFoundErrorOrPanic ( conn . QueryRow ( ctx , `
select quote_id
, decimal_digits
, quote_number
, quote_date
, terms_and_conditions
, notes
, coalesce ( instructions , ' ' )
Allow empty contact and payment method for quotes
I have to use a value to be used as “none” for payment method and
contact. In PL/pgSQL add_quote and edit_quote functions, that value is
NULL, while in forms it is the empty string. I can not simply pass the
empty string for either of these fields because PL/pgSQL expects
(nullable) integers, and "" is not a valid integer and is not NULL
either. A conversion is necessary.
Apparently, Go’s nil is not a valid representation for SQL’s NULL with
pgx, and had to use sql.NullString instead.
I also needed to coalesce contact’s VATIN and phone, because null values
can not be scanned to *string. I did not do that before because
`coalesce(vatin, '')` throws an error that '' is not a valid VATIN and
just left as is, wrongly expecting that pgx would do the job of leaving
the string blank for me. It does not.
Lastly, i can not blindly write Quotee’s tax details in the quote’s view
page, or we would see the (), characters for the empty address info.
2023-06-08 11:05:41 +00:00
, contact_id is not null
Split contact relation into tax_details, phone, web, and email
We need to have contacts with just a name: we need to assign
freelancer’s quote as expense linked the government, but of course we
do not have a phone or email for that “contact”, much less a VATIN or
other tax details.
It is also interesting for other expenses-only contacts to not have to
input all tax details, as we may not need to invoice then, thus are
useless for us, but sometimes it might be interesting to have them,
“just in case”.
Of course, i did not want to make nullable any of the tax details
required to generate an invoice, otherwise we could allow illegal
invoices. Therefore, that data had to go in a different relation,
and invoice’s foreign key update to point to that relation, not just
customer, or we would again be able to create invalid invoices.
We replaced the contact’s trade name with just name, because we do not
need _three_ names for a contact, but we _do_ need two: the one we use
to refer to them and the business name for tax purposes.
The new contact_phone, contact_web, and contact_email relations could be
simply a nullable field, but i did not see the point, since there are
not that many instances where i need any of this data.
Now company.taxDetailsForm is no longer “the same as contactForm with
some extra fields”, because i have to add a check whether the user needs
to invoice the contact, to check that the required values are there.
I have an additional problem with the contact form when not using
JavaScript: i must set the required field to all tax details fields to
avoid the “(optional)” suffix, and because they _are_ required when
that checkbox is enabled, but i can not set them optional when the check
is unchecked. My solution for now is to ignore the form validation,
and later i will add some JavaScript that adds the validation again,
so it will work in all cases.
2023-06-30 19:32:48 +00:00
, coalesce ( business_name , contact . name , ' ' )
, contact_tax_details . contact_id is not null
Allow empty contact and payment method for quotes
I have to use a value to be used as “none” for payment method and
contact. In PL/pgSQL add_quote and edit_quote functions, that value is
NULL, while in forms it is the empty string. I can not simply pass the
empty string for either of these fields because PL/pgSQL expects
(nullable) integers, and "" is not a valid integer and is not NULL
either. A conversion is necessary.
Apparently, Go’s nil is not a valid representation for SQL’s NULL with
pgx, and had to use sql.NullString instead.
I also needed to coalesce contact’s VATIN and phone, because null values
can not be scanned to *string. I did not do that before because
`coalesce(vatin, '')` throws an error that '' is not a valid VATIN and
just left as is, wrongly expecting that pgx would do the job of leaving
the string blank for me. It does not.
Lastly, i can not blindly write Quotee’s tax details in the quote’s view
page, or we would see the (), characters for the empty address info.
2023-06-08 11:05:41 +00:00
, coalesce ( vatin : : text , ' ' )
2023-06-07 14:35:31 +00:00
, coalesce ( address , ' ' )
, coalesce ( city , ' ' )
, coalesce ( province , ' ' )
, coalesce ( postal_code , ' ' )
, to_price ( subtotal , decimal_digits )
, to_price ( total , decimal_digits )
from quote
left join quote_payment_method using ( quote_id )
left join payment_method using ( payment_method_id )
left join quote_contact using ( quote_id )
left join contact using ( contact_id )
Split contact relation into tax_details, phone, web, and email
We need to have contacts with just a name: we need to assign
freelancer’s quote as expense linked the government, but of course we
do not have a phone or email for that “contact”, much less a VATIN or
other tax details.
It is also interesting for other expenses-only contacts to not have to
input all tax details, as we may not need to invoice then, thus are
useless for us, but sometimes it might be interesting to have them,
“just in case”.
Of course, i did not want to make nullable any of the tax details
required to generate an invoice, otherwise we could allow illegal
invoices. Therefore, that data had to go in a different relation,
and invoice’s foreign key update to point to that relation, not just
customer, or we would again be able to create invalid invoices.
We replaced the contact’s trade name with just name, because we do not
need _three_ names for a contact, but we _do_ need two: the one we use
to refer to them and the business name for tax purposes.
The new contact_phone, contact_web, and contact_email relations could be
simply a nullable field, but i did not see the point, since there are
not that many instances where i need any of this data.
Now company.taxDetailsForm is no longer “the same as contactForm with
some extra fields”, because i have to add a check whether the user needs
to invoice the contact, to check that the required values are there.
I have an additional problem with the contact form when not using
JavaScript: i must set the required field to all tax details fields to
avoid the “(optional)” suffix, and because they _are_ required when
that checkbox is enabled, but i can not set them optional when the check
is unchecked. My solution for now is to ignore the form validation,
and later i will add some JavaScript that adds the validation again,
so it will work in all cases.
2023-06-30 19:32:48 +00:00
left join contact_tax_details using ( contact_id )
2023-06-07 14:35:31 +00:00
join quote_amount using ( quote_id )
join currency using ( currency_code )
where quote . slug = $ 1 ` , slug ) . Scan (
& quoteId ,
& decimalDigits ,
& quo . Number ,
& quo . Date ,
& quo . TermsAndConditions ,
& quo . Notes ,
& quo . PaymentInstructions ,
Allow empty contact and payment method for quotes
I have to use a value to be used as “none” for payment method and
contact. In PL/pgSQL add_quote and edit_quote functions, that value is
NULL, while in forms it is the empty string. I can not simply pass the
empty string for either of these fields because PL/pgSQL expects
(nullable) integers, and "" is not a valid integer and is not NULL
either. A conversion is necessary.
Apparently, Go’s nil is not a valid representation for SQL’s NULL with
pgx, and had to use sql.NullString instead.
I also needed to coalesce contact’s VATIN and phone, because null values
can not be scanned to *string. I did not do that before because
`coalesce(vatin, '')` throws an error that '' is not a valid VATIN and
just left as is, wrongly expecting that pgx would do the job of leaving
the string blank for me. It does not.
Lastly, i can not blindly write Quotee’s tax details in the quote’s view
page, or we would see the (), characters for the empty address info.
2023-06-08 11:05:41 +00:00
& quo . HasQuotee ,
2023-06-07 14:35:31 +00:00
& quo . Quotee . Name ,
Split contact relation into tax_details, phone, web, and email
We need to have contacts with just a name: we need to assign
freelancer’s quote as expense linked the government, but of course we
do not have a phone or email for that “contact”, much less a VATIN or
other tax details.
It is also interesting for other expenses-only contacts to not have to
input all tax details, as we may not need to invoice then, thus are
useless for us, but sometimes it might be interesting to have them,
“just in case”.
Of course, i did not want to make nullable any of the tax details
required to generate an invoice, otherwise we could allow illegal
invoices. Therefore, that data had to go in a different relation,
and invoice’s foreign key update to point to that relation, not just
customer, or we would again be able to create invalid invoices.
We replaced the contact’s trade name with just name, because we do not
need _three_ names for a contact, but we _do_ need two: the one we use
to refer to them and the business name for tax purposes.
The new contact_phone, contact_web, and contact_email relations could be
simply a nullable field, but i did not see the point, since there are
not that many instances where i need any of this data.
Now company.taxDetailsForm is no longer “the same as contactForm with
some extra fields”, because i have to add a check whether the user needs
to invoice the contact, to check that the required values are there.
I have an additional problem with the contact form when not using
JavaScript: i must set the required field to all tax details fields to
avoid the “(optional)” suffix, and because they _are_ required when
that checkbox is enabled, but i can not set them optional when the check
is unchecked. My solution for now is to ignore the form validation,
and later i will add some JavaScript that adds the validation again,
so it will work in all cases.
2023-06-30 19:32:48 +00:00
& quo . HasTaxDetails ,
2023-06-07 14:35:31 +00:00
& quo . Quotee . VATIN ,
& quo . Quotee . Address ,
& quo . Quotee . City ,
& quo . Quotee . Province ,
& quo . Quotee . PostalCode ,
& quo . Subtotal ,
& quo . Total ) ) {
return nil
}
Keep products in invoices/quotes sorted by (roughly) insertion order
There was no explicit `order by` in the queries that list the products
of quotes and invoices, so PostgreSQL was free to use any order it
wanted. In this case, since was am grouping first by name, the result
was sorted by product name.
This is not an issue in most cases, albeit a bit rude to the user,
except for when the products *have* to in the same order the user
entered them, because they are monthly fees or something like that, that
must be ordered by month _number_, not by their _name_; the user will
usually input them in the correct order they want them on the invoice or
quote.
Sorting by *_product_id does *not* guarantee that they will always be
in insertion order, because the sequence can “wrap”, but i think i am
going to have bigger problems at that point.
Closes #63
2023-07-07 08:39:22 +00:00
if err := conn . QueryRow ( ctx , `
select business_name
, vatin , phone
, email
, address
, city
, province
, postal_code
, legal_disclaimer
from company
where company_id = $ 1
` , company . Id ) . Scan (
& quo . Quoter . Name ,
& quo . Quoter . VATIN ,
& quo . Quoter . Phone ,
& quo . Quoter . Email ,
& quo . Quoter . Address ,
& quo . Quoter . City ,
& quo . Quoter . Province ,
& quo . Quoter . PostalCode ,
& quo . LegalDisclaimer ) ; err != nil {
2023-06-07 14:35:31 +00:00
panic ( err )
}
Keep products in invoices/quotes sorted by (roughly) insertion order
There was no explicit `order by` in the queries that list the products
of quotes and invoices, so PostgreSQL was free to use any order it
wanted. In this case, since was am grouping first by name, the result
was sorted by product name.
This is not an issue in most cases, albeit a bit rude to the user,
except for when the products *have* to in the same order the user
entered them, because they are monthly fees or something like that, that
must be ordered by month _number_, not by their _name_; the user will
usually input them in the correct order they want them on the invoice or
quote.
Sorting by *_product_id does *not* guarantee that they will always be
in insertion order, because the sequence can “wrap”, but i think i am
going to have bigger problems at that point.
Closes #63
2023-07-07 08:39:22 +00:00
if err := conn . QueryRow ( ctx , `
select array_agg ( array [ name , to_price ( amount , $ 2 ) ] ) from quote_tax_amount
join tax using ( tax_id )
where quote_id = $ 1
` , quoteId , decimalDigits ) . Scan ( & quo . Taxes ) ; err != nil {
2023-06-07 14:35:31 +00:00
panic ( err )
}
Keep products in invoices/quotes sorted by (roughly) insertion order
There was no explicit `order by` in the queries that list the products
of quotes and invoices, so PostgreSQL was free to use any order it
wanted. In this case, since was am grouping first by name, the result
was sorted by product name.
This is not an issue in most cases, albeit a bit rude to the user,
except for when the products *have* to in the same order the user
entered them, because they are monthly fees or something like that, that
must be ordered by month _number_, not by their _name_; the user will
usually input them in the correct order they want them on the invoice or
quote.
Sorting by *_product_id does *not* guarantee that they will always be
in insertion order, because the sequence can “wrap”, but i think i am
going to have bigger problems at that point.
Closes #63
2023-07-07 08:39:22 +00:00
rows := conn . MustQuery ( ctx , `
select quote_product . name
, description
, to_price ( price , $ 2 )
, ( discount_rate * 100 ) : : integer
, quantity , to_price ( subtotal , $ 2 )
, to_price ( total , $ 2 )
, array_agg ( array [ tax_class . name , ( tax_rate * 100 ) : : integer : : text ] ) filter ( where tax_rate is not null )
from quote_product
join quote_product_amount using ( quote_product_id )
left join quote_product_tax using ( quote_product_id )
left join tax using ( tax_id )
left join tax_class using ( tax_class_id )
where quote_id = $ 1
group by quote_product_id
, quote_product . name
, description
, discount_rate
, price
, quantity
, subtotal
, total
order by quote_product_id
` , quoteId , decimalDigits )
2023-06-07 14:35:31 +00:00
defer rows . Close ( )
taxClasses := map [ string ] bool { }
for rows . Next ( ) {
product := & quoteProduct {
Taxes : make ( map [ string ] int ) ,
}
var taxes [ ] [ ] string
Keep products in invoices/quotes sorted by (roughly) insertion order
There was no explicit `order by` in the queries that list the products
of quotes and invoices, so PostgreSQL was free to use any order it
wanted. In this case, since was am grouping first by name, the result
was sorted by product name.
This is not an issue in most cases, albeit a bit rude to the user,
except for when the products *have* to in the same order the user
entered them, because they are monthly fees or something like that, that
must be ordered by month _number_, not by their _name_; the user will
usually input them in the correct order they want them on the invoice or
quote.
Sorting by *_product_id does *not* guarantee that they will always be
in insertion order, because the sequence can “wrap”, but i think i am
going to have bigger problems at that point.
Closes #63
2023-07-07 08:39:22 +00:00
if err := rows . Scan (
& product . Name ,
& product . Description ,
& product . Price ,
& product . Discount ,
& product . Quantity ,
& product . Subtotal ,
& product . Total ,
& taxes ) ; err != nil {
2023-06-07 14:35:31 +00:00
panic ( err )
}
for _ , tax := range taxes {
taxClass := tax [ 0 ]
taxClasses [ taxClass ] = true
product . Taxes [ taxClass ] , _ = strconv . Atoi ( tax [ 1 ] )
}
if product . Discount > 0 {
quo . HasDiscounts = true
}
quo . Products = append ( quo . Products , product )
}
for taxClass := range taxClasses {
quo . TaxClasses = append ( quo . TaxClasses , taxClass )
}
sort . Strings ( quo . TaxClasses )
if rows . Err ( ) != nil {
panic ( rows . Err ( ) )
}
return quo
}
type newQuotePage struct {
Form * quoteForm
Subtotal string
Taxes [ ] [ ] string
Total string
}
func newNewQuotePage ( form * quoteForm , r * http . Request ) * newQuotePage {
page := & newQuotePage {
Form : form ,
}
conn := getConn ( r )
company := mustGetCompany ( r )
err := conn . QueryRow ( r . Context ( ) , "select subtotal, taxes, total from compute_new_quote_amount($1, $2)" , company . Id , NewQuoteProductArray ( form . Products ) ) . Scan ( & page . Subtotal , & page . Taxes , & page . Total )
if err != nil {
panic ( err )
}
if len ( form . Products ) == 0 {
form . Products = append ( form . Products , newQuoteProductForm ( 0 , company , getLocale ( r ) , mustGetTaxOptions ( r . Context ( ) , conn , company ) ) )
}
return page
}
func mustRenderNewQuoteForm ( w http . ResponseWriter , r * http . Request , form * quoteForm ) {
page := newNewQuotePage ( form , r )
mustRenderMainTemplate ( w , r , "quotes/new.gohtml" , page )
}
func mustRenderNewQuoteProductsForm ( w http . ResponseWriter , r * http . Request , action string , form * quoteForm ) {
conn := getConn ( r )
company := mustGetCompany ( r )
page := newQuoteProductsPage {
Action : companyURI ( company , action ) ,
Form : form ,
Products : mustGetProductChoices ( r . Context ( ) , conn , company ) ,
}
mustRenderMainTemplate ( w , r , "quotes/products.gohtml" , page )
}
type newQuoteProductsPage struct {
Action string
Form * quoteForm
Products [ ] * productChoice
}
func HandleAddQuote ( w http . ResponseWriter , r * http . Request , _ httprouter . Params ) {
locale := getLocale ( r )
conn := getConn ( r )
company := mustGetCompany ( r )
form := newQuoteForm ( r . Context ( ) , conn , locale , company )
if err := form . Parse ( r ) ; err != nil {
http . Error ( w , err . Error ( ) , http . StatusBadRequest )
return
}
if err := verifyCsrfTokenValid ( r ) ; err != nil {
http . Error ( w , err . Error ( ) , http . StatusForbidden )
return
}
if ! form . Validate ( ) {
if ! IsHTMxRequest ( r ) {
w . WriteHeader ( http . StatusUnprocessableEntity )
}
mustRenderNewQuoteForm ( w , r , form )
return
}
Allow empty contact and payment method for quotes
I have to use a value to be used as “none” for payment method and
contact. In PL/pgSQL add_quote and edit_quote functions, that value is
NULL, while in forms it is the empty string. I can not simply pass the
empty string for either of these fields because PL/pgSQL expects
(nullable) integers, and "" is not a valid integer and is not NULL
either. A conversion is necessary.
Apparently, Go’s nil is not a valid representation for SQL’s NULL with
pgx, and had to use sql.NullString instead.
I also needed to coalesce contact’s VATIN and phone, because null values
can not be scanned to *string. I did not do that before because
`coalesce(vatin, '')` throws an error that '' is not a valid VATIN and
just left as is, wrongly expecting that pgx would do the job of leaving
the string blank for me. It does not.
Lastly, i can not blindly write Quotee’s tax details in the quote’s view
page, or we would see the (), characters for the empty address info.
2023-06-08 11:05:41 +00:00
slug := conn . MustGetText ( r . Context ( ) , "" , "select add_quote($1, $2, $3, $4, $5, $6, $7, $8)" , company . Id , form . Date , form . Customer . OrNull ( ) , form . TermsAndConditions , form . Notes , form . PaymentMethod . OrNull ( ) , form . Tags , NewQuoteProductArray ( form . Products ) )
2023-06-07 14:35:31 +00:00
htmxRedirect ( w , r , companyURI ( company , "/quotes/" + slug ) )
}
func HandleNewQuoteAction ( w http . ResponseWriter , r * http . Request , params httprouter . Params ) {
switch params [ 0 ] . Value {
case "new" :
handleQuoteAction ( w , r , "/quotes/new" , mustRenderNewQuoteForm )
case "batch" :
HandleBatchQuoteAction ( w , r , params )
default :
http . Error ( w , "Method Not Allowed" , http . StatusMethodNotAllowed )
}
}
func HandleBatchQuoteAction ( w http . ResponseWriter , r * http . Request , _ httprouter . Params ) {
if err := r . ParseForm ( ) ; err != nil {
http . Error ( w , err . Error ( ) , http . StatusBadRequest )
return
}
if err := verifyCsrfTokenValid ( r ) ; err != nil {
http . Error ( w , err . Error ( ) , http . StatusForbidden )
return
}
locale := getLocale ( r )
switch r . Form . Get ( "action" ) {
case "download" :
Add option to export the list of quotes, invoices, and expenses to ODS
This was requested by a potential user, as they want to be able to do
whatever they want to do to these lists with a spreadsheet.
In fact, they requested to be able to export to CSV, but, as always,
using CSV is a minefield because of Microsoft: since their Excel product
is fucking unable to write and read CSV from different locales, even if
using the same exact Excel product, i can not also create a CSV file
that is guaranteed to work on all locales. If i used the non-standard
sep=; thing to tell Excel that it is a fucking stupid application, then
proper applications would show that line as a row, which is the correct
albeit undesirable behaviour.
The solution is to use a spreadsheet file format that does not have this
issue. As far as I know, by default Excel is able to read XLSX and ODS
files, but i refuse to use the artificially complex, not the actually
used in Excel, and lobbied standard that Microsoft somehow convinced ISO
to publish, as i am using a different format because of the mess they
made, and i do not want to bend over in front of them, so ODS it is.
ODS is neither an elegant or good format by any means, but at least i
can write them using simple strings, because there is no ODS library
in Debian and i am not going to write yet another DEB package for an
overengineered package to write a simple table—all i want is to say
“here are these n columns, and these m columns; have a good day!”.
Part of #51.
2023-07-18 11:29:36 +00:00
slugs := r . Form [ "quote" ]
if len ( slugs ) == 0 {
http . Redirect ( w , r , companyURI ( mustGetCompany ( r ) , "/quotes" ) , http . StatusSeeOther )
return
}
2023-06-07 14:35:31 +00:00
quotes := mustWriteQuotesPdf ( r , slugs )
w . Header ( ) . Set ( "Content-Type" , "application/zip" )
w . Header ( ) . Set ( "Content-Disposition" , fmt . Sprintf ( "attachment; filename=%s" , gettext ( "quotations.zip" , locale ) ) )
w . WriteHeader ( http . StatusOK )
if _ , err := w . Write ( quotes ) ; err != nil {
panic ( err )
}
Add option to export the list of quotes, invoices, and expenses to ODS
This was requested by a potential user, as they want to be able to do
whatever they want to do to these lists with a spreadsheet.
In fact, they requested to be able to export to CSV, but, as always,
using CSV is a minefield because of Microsoft: since their Excel product
is fucking unable to write and read CSV from different locales, even if
using the same exact Excel product, i can not also create a CSV file
that is guaranteed to work on all locales. If i used the non-standard
sep=; thing to tell Excel that it is a fucking stupid application, then
proper applications would show that line as a row, which is the correct
albeit undesirable behaviour.
The solution is to use a spreadsheet file format that does not have this
issue. As far as I know, by default Excel is able to read XLSX and ODS
files, but i refuse to use the artificially complex, not the actually
used in Excel, and lobbied standard that Microsoft somehow convinced ISO
to publish, as i am using a different format because of the mess they
made, and i do not want to bend over in front of them, so ODS it is.
ODS is neither an elegant or good format by any means, but at least i
can write them using simple strings, because there is no ODS library
in Debian and i am not going to write yet another DEB package for an
overengineered package to write a simple table—all i want is to say
“here are these n columns, and these m columns; have a good day!”.
Part of #51.
2023-07-18 11:29:36 +00:00
case "export" :
conn := getConn ( r )
company := getCompany ( r )
filters := newQuoteFilterForm ( r . Context ( ) , conn , locale , company )
if err := filters . Parse ( r ) ; err != nil {
http . Error ( w , err . Error ( ) , http . StatusBadRequest )
return
}
ods := mustWriteQuotesOds ( mustCollectQuoteEntries ( r . Context ( ) , conn , locale , filters ) , locale , company )
writeOdsResponse ( w , ods , gettext ( "quotations.ods" , locale ) )
2023-06-07 14:35:31 +00:00
default :
http . Error ( w , gettext ( "Invalid action" , locale ) , http . StatusBadRequest )
}
}
func mustWriteQuotesPdf ( r * http . Request , slugs [ ] string ) [ ] byte {
conn := getConn ( r )
company := mustGetCompany ( r )
buf := new ( bytes . Buffer )
w := zip . NewWriter ( buf )
for _ , slug := range slugs {
quo := mustGetQuote ( r . Context ( ) , conn , company , slug )
if quo == nil {
continue
}
f , err := w . Create ( quo . Number + ".pdf" )
if err != nil {
panic ( err )
}
mustWriteQuotePdf ( f , r , quo )
}
mustClose ( w )
return buf . Bytes ( )
}
type quoteForm struct {
locale * Locale
company * Company
Number string
QuoteStatus * SelectField
Customer * SelectField
Date * InputField
TermsAndConditions * InputField
Notes * InputField
PaymentMethod * SelectField
Tags * TagsField
Products [ ] * quoteProductForm
RemovedProduct * quoteProductForm
}
func newQuoteForm ( ctx context . Context , conn * Conn , locale * Locale , company * Company ) * quoteForm {
return & quoteForm {
locale : locale ,
company : company ,
QuoteStatus : & SelectField {
Name : "quote_status" ,
Required : true ,
Label : pgettext ( "input" , "Quotation Status" , locale ) ,
Selected : [ ] string { "created" } ,
Options : MustGetOptions ( ctx , conn , "select quote_status.quote_status, isi18n.name from quote_status join quote_status_i18n isi18n using(quote_status) where isi18n.lang_tag = $1 order by quote_status" , locale . Language . String ( ) ) ,
} ,
Customer : & SelectField {
Name : "customer" ,
2023-06-20 09:37:02 +00:00
Label : pgettext ( "input" , "Customer" , locale ) ,
2023-06-07 14:35:31 +00:00
EmptyLabel : gettext ( "Select a customer to quote." , locale ) ,
Options : mustGetContactOptions ( ctx , conn , company ) ,
} ,
Date : & InputField {
Name : "date" ,
Label : pgettext ( "input" , "Quotation Date" , locale ) ,
Type : "date" ,
Required : true ,
} ,
TermsAndConditions : & InputField {
Name : "terms_and_conditions" ,
Label : pgettext ( "input" , "Terms and conditions" , locale ) ,
Type : "textarea" ,
} ,
Notes : & InputField {
Name : "notes" ,
Label : pgettext ( "input" , "Notes" , locale ) ,
Type : "textarea" ,
} ,
Tags : & TagsField {
Name : "tags" ,
Label : pgettext ( "input" , "Tags" , locale ) ,
} ,
PaymentMethod : & SelectField {
Name : "payment_method" ,
Label : pgettext ( "input" , "Payment Method" , locale ) ,
EmptyLabel : gettext ( "Select a payment method." , locale ) ,
Options : mustGetPaymentMethodOptions ( ctx , conn , company ) ,
} ,
}
}
func ( form * quoteForm ) Parse ( r * http . Request ) error {
if err := r . ParseForm ( ) ; err != nil {
return err
}
form . QuoteStatus . FillValue ( r )
form . Customer . FillValue ( r )
form . Date . FillValue ( r )
2023-06-08 10:50:16 +00:00
form . TermsAndConditions . FillValue ( r )
2023-06-07 14:35:31 +00:00
form . Notes . FillValue ( r )
form . Tags . FillValue ( r )
form . PaymentMethod . FillValue ( r )
if _ , ok := r . Form [ "product.id.0" ] ; ok {
taxOptions := mustGetTaxOptions ( r . Context ( ) , getConn ( r ) , form . company )
for index := 0 ; true ; index ++ {
if _ , ok := r . Form [ "product.id." + strconv . Itoa ( index ) ] ; ! ok {
break
}
productForm := newQuoteProductForm ( index , form . company , form . locale , taxOptions )
if err := productForm . Parse ( r ) ; err != nil {
return err
}
form . Products = append ( form . Products , productForm )
}
}
return nil
}
func ( form * quoteForm ) Validate ( ) bool {
validator := newFormValidator ( )
validator . CheckValidSelectOption ( form . QuoteStatus , gettext ( "Selected quotation status is not valid." , form . locale ) )
if form . Customer . String ( ) != "" {
validator . CheckValidSelectOption ( form . Customer , gettext ( "Selected customer is not valid." , form . locale ) )
}
if validator . CheckRequiredInput ( form . Date , gettext ( "Quotation date can not be empty." , form . locale ) ) {
validator . CheckValidDate ( form . Date , gettext ( "Quotation date must be a valid date." , form . locale ) )
}
if form . PaymentMethod . String ( ) != "" {
validator . CheckValidSelectOption ( form . PaymentMethod , gettext ( "Selected payment method is not valid." , form . locale ) )
}
allOK := validator . AllOK ( )
for _ , product := range form . Products {
allOK = product . Validate ( ) && allOK
}
return allOK
}
func ( form * quoteForm ) Update ( ) {
products := form . Products
form . Products = nil
for n , product := range products {
if product . Quantity . Val != "0" {
product . Update ( )
if n != len ( form . Products ) {
product . Index = len ( form . Products )
product . Rename ( )
}
form . Products = append ( form . Products , product )
}
}
}
func ( form * quoteForm ) RemoveProduct ( index int ) {
products := form . Products
form . Products = nil
for n , product := range products {
if n == index {
form . RemovedProduct = product
} else {
if n != len ( form . Products ) {
product . Index = len ( form . Products )
product . Rename ( )
}
form . Products = append ( form . Products , product )
}
}
if form . RemovedProduct != nil {
form . RemovedProduct . RenameWithSuffix ( removedProductSuffix )
}
}
func ( form * quoteForm ) AddProducts ( ctx context . Context , conn * Conn , productsSlug [ ] string ) {
form . mustAddProductsFromQuery ( ctx , conn , selectProductBySlug , productsSlug )
}
func ( form * quoteForm ) mustAddProductsFromQuery ( ctx context . Context , conn * Conn , sql string , args ... interface { } ) {
index := len ( form . Products )
taxOptions := mustGetTaxOptions ( ctx , conn , form . company )
rows := conn . MustQuery ( ctx , sql , args ... )
defer rows . Close ( )
for rows . Next ( ) {
product := newQuoteProductForm ( index , form . company , form . locale , taxOptions )
if err := rows . Scan ( product . QuoteProductId , 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 ( form * quoteForm ) InsertProduct ( product * quoteProductForm ) {
replaced := false
for n , existing := range form . Products {
if existing . Quantity . Val == "" || existing . Quantity . Val == "0" {
product . Index = n
form . Products [ n ] = product
replaced = true
break
}
}
if ! replaced {
product . Index = len ( form . Products )
form . Products = append ( form . Products , product )
}
product . Rename ( )
}
func ( form * quoteForm ) MustFillFromDatabase ( ctx context . Context , conn * Conn , slug string ) bool {
var quoteId int
selectedQuoteStatus := form . QuoteStatus . Selected
form . QuoteStatus . Clear ( )
selectedPaymentMethod := form . PaymentMethod . Selected
form . PaymentMethod . Clear ( )
if notFoundErrorOrPanic ( conn . QueryRow ( ctx , `
select quote_id
, quote_status
, contact_id
, quote_number
, quote_date
2023-06-08 10:50:16 +00:00
, terms_and_conditions
2023-06-07 14:35:31 +00:00
, notes
, payment_method_id
, tags
from quote
left join quote_contact using ( quote_id )
left join quote_payment_method using ( quote_id )
where slug = $ 1
2023-06-08 10:50:16 +00:00
` , slug ) . Scan ( & quoteId , form . QuoteStatus , form . Customer , & form . Number , form . Date , form . TermsAndConditions , form . Notes , form . PaymentMethod , form . Tags ) ) {
2023-06-07 14:35:31 +00:00
form . PaymentMethod . Selected = selectedPaymentMethod
form . QuoteStatus . Selected = selectedQuoteStatus
return false
}
form . Products = [ ] * quoteProductForm { }
form . mustAddProductsFromQuery ( ctx , conn , "select quote_product_id::text, coalesce(product_id, 0), name, description, to_price(price, $2), quantity, (discount_rate * 100)::integer, array_remove(array_agg(tax_id), null) from quote_product left join quote_product_product using (quote_product_id) left join quote_product_tax using (quote_product_id) where quote_id = $1 group by quote_product_id, coalesce(product_id, 0), name, description, discount_rate, price, quantity" , quoteId , form . company . DecimalDigits )
return true
}
type quoteProductForm struct {
locale * Locale
company * Company
Index int
QuoteProductId * InputField
ProductId * InputField
Name * InputField
Description * InputField
Price * InputField
Quantity * InputField
Discount * InputField
Tax * SelectField
}
func newQuoteProductForm ( index int , company * Company , locale * Locale , taxOptions [ ] * SelectOption ) * quoteProductForm {
triggerRecompute := template . HTMLAttr ( ` data-hx-on="change: this.dispatchEvent(new CustomEvent('recompute', { bubbles: true}))" ` )
form := & quoteProductForm {
locale : locale ,
company : company ,
Index : index ,
QuoteProductId : & InputField {
Label : pgettext ( "input" , "Id" , locale ) ,
Type : "hidden" ,
Required : true ,
} ,
ProductId : & InputField {
Label : pgettext ( "input" , "Id" , locale ) ,
Type : "hidden" ,
Required : true ,
} ,
Name : & InputField {
Label : pgettext ( "input" , "Name" , locale ) ,
Type : "text" ,
Required : true ,
Is : "numerus-product-search" ,
Attributes : [ ] template . HTMLAttr {
` autocomplete="off" ` ,
` data-hx-trigger="keyup changed delay:200" ` ,
` data-hx-target="next .options" ` ,
` data-hx-indicator="closest div" ` ,
` data-hx-swap="innerHTML" ` ,
template . HTMLAttr ( fmt . Sprintf ( ` data-hx-get="%v" ` , companyURI ( company , "/search/products" ) ) ) ,
} ,
} ,
Description : & InputField {
Label : pgettext ( "input" , "Description" , locale ) ,
Type : "textarea" ,
} ,
Price : & InputField {
Label : pgettext ( "input" , "Price" , locale ) ,
Type : "number" ,
Required : true ,
Attributes : [ ] template . HTMLAttr {
triggerRecompute ,
` min="0" ` ,
template . HTMLAttr ( fmt . Sprintf ( ` step="%v" ` , company . MinCents ( ) ) ) ,
} ,
} ,
Quantity : & InputField {
Label : pgettext ( "input" , "Quantity" , locale ) ,
Type : "number" ,
Required : true ,
Attributes : [ ] template . HTMLAttr {
triggerRecompute ,
` min="0" ` ,
} ,
} ,
Discount : & InputField {
Label : pgettext ( "input" , "Discount (%)" , locale ) ,
Type : "number" ,
Required : true ,
Attributes : [ ] template . HTMLAttr {
triggerRecompute ,
` min="0" ` ,
` max="100" ` ,
} ,
} ,
Tax : & SelectField {
Label : pgettext ( "input" , "Taxes" , locale ) ,
Multiple : true ,
Options : taxOptions ,
Attributes : [ ] template . HTMLAttr {
triggerRecompute ,
} ,
} ,
}
form . Rename ( )
return form
}
func ( form * quoteProductForm ) Rename ( ) {
form . RenameWithSuffix ( "." + strconv . Itoa ( form . Index ) )
}
func ( form * quoteProductForm ) RenameWithSuffix ( suffix string ) {
form . QuoteProductId . Name = "product.quote_product_id" + suffix
form . ProductId . Name = "product.id" + suffix
form . Name . Name = "product.name" + suffix
form . Description . Name = "product.description" + suffix
form . Price . Name = "product.price" + suffix
form . Quantity . Name = "product.quantity" + suffix
form . Discount . Name = "product.discount" + suffix
form . Tax . Name = "product.tax" + suffix
}
func ( form * quoteProductForm ) Parse ( r * http . Request ) error {
if err := r . ParseForm ( ) ; err != nil {
return err
}
form . QuoteProductId . FillValue ( r )
form . ProductId . FillValue ( r )
form . Name . FillValue ( r )
form . Description . FillValue ( r )
form . Price . FillValue ( r )
form . Quantity . FillValue ( r )
form . Discount . FillValue ( r )
form . Tax . FillValue ( r )
return nil
}
func ( form * quoteProductForm ) Validate ( ) bool {
validator := newFormValidator ( )
if form . QuoteProductId . Val != "" {
validator . CheckValidInteger ( form . QuoteProductId , 1 , math . MaxInt32 , gettext ( "Quotation product ID must be a number greater than zero." , form . locale ) )
}
if form . ProductId . Val != "" {
validator . CheckValidInteger ( form . ProductId , 0 , math . MaxInt32 , gettext ( "Product ID must be a positive number or zero." , form . locale ) )
}
validator . CheckRequiredInput ( form . Name , gettext ( "Name can not be empty." , form . locale ) )
if validator . CheckRequiredInput ( form . Price , gettext ( "Price can not be empty." , form . locale ) ) {
validator . CheckValidDecimal ( form . Price , form . company . MinCents ( ) , math . MaxFloat64 , gettext ( "Price must be a number greater than zero." , form . locale ) )
}
if validator . CheckRequiredInput ( form . Quantity , gettext ( "Quantity can not be empty." , form . locale ) ) {
validator . CheckValidInteger ( form . Quantity , 1 , math . MaxInt32 , gettext ( "Quantity must be a number greater than zero." , form . locale ) )
}
if validator . CheckRequiredInput ( form . Discount , gettext ( "Discount can not be empty." , form . locale ) ) {
validator . CheckValidInteger ( form . Discount , 0 , 100 , gettext ( "Discount must be a percentage between 0 and 100." , form . locale ) )
}
validator . CheckValidSelectOption ( form . Tax , gettext ( "Selected tax is not valid." , form . locale ) )
validator . CheckAtMostOneOfEachGroup ( form . Tax , gettext ( "You can only select a tax of each class." , form . locale ) )
return validator . AllOK ( )
}
func ( form * quoteProductForm ) Update ( ) {
validator := newFormValidator ( )
if ! validator . CheckValidDecimal ( form . Price , form . company . MinCents ( ) , math . MaxFloat64 , "" ) {
form . Price . Val = "0.0"
form . Price . Errors = nil
}
if ! validator . CheckValidInteger ( form . Quantity , 0 , math . MaxInt32 , "" ) {
form . Quantity . Val = "1"
form . Quantity . Errors = nil
}
if ! validator . CheckValidInteger ( form . Discount , 0 , 100 , "" ) {
form . Discount . Val = "0"
form . Discount . Errors = nil
}
}
func ( form * quoteProductForm ) MustFillFromDatabase ( ctx context . Context , conn * Conn , slug string ) bool {
return ! notFoundErrorOrPanic ( conn . QueryRow ( ctx , selectProductBySlug , [ ] string { slug } ) . Scan (
form . QuoteProductId ,
form . ProductId ,
form . Name ,
form . Description ,
form . Price ,
form . Quantity ,
form . Discount ,
form . Tax ) )
}
func HandleUpdateQuote ( w http . ResponseWriter , r * http . Request , params httprouter . Params ) {
locale := getLocale ( r )
conn := getConn ( r )
company := mustGetCompany ( r )
form := newQuoteForm ( r . Context ( ) , conn , locale , company )
if err := form . Parse ( r ) ; err != nil {
http . Error ( w , err . Error ( ) , http . StatusBadRequest )
return
}
if err := verifyCsrfTokenValid ( r ) ; err != nil {
http . Error ( w , err . Error ( ) , http . StatusForbidden )
return
}
Return HTTP 404 instead of 500 for invalid UUID values in URL
Since most of PL/pgSQL functions accept a `uuid` domain, we get an error
if the value is not valid, forcing us to return an HTTP 500, as we
can not detect that the error was due to that.
Instead, i now validate that the slug is indeed a valid UUID before
attempting to send it to the database, returning the correct HTTP error
code and avoiding useless calls to the database.
I based the validation function of Parse() from Google’s uuid package[0]
because this function is an order or magnitude faster in benchmarks:
goos: linux
goarch: amd64
pkg: dev.tandem.ws/tandem/numerus/pkg
cpu: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
BenchmarkValidUuid-4 36946050 29.37 ns/op
BenchmarkValidUuid_Re-4 3633169 306.70 ns/op
The regular expression used for the benchmark was:
var re = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
And the input parameter for both functions was the following valid UUID,
because most of the time the passed UUID will be valid:
"f47ac10b-58cc-0372-8567-0e02b2c3d479"
I did not use the uuid package, even though it is in Debian’s
repository, because i only need to check whether the value is valid,
not convert it to a byte array. As far as i know, that package can not
do that.
[0]: https://github.com/google/uuid
2023-07-17 09:46:11 +00:00
slug := params [ 0 ] . Value
if ! ValidUuid ( slug ) {
http . NotFound ( w , r )
return
}
2023-06-07 14:35:31 +00:00
if r . FormValue ( "quick" ) == "status" {
Return HTTP 404 instead of 500 for invalid UUID values in URL
Since most of PL/pgSQL functions accept a `uuid` domain, we get an error
if the value is not valid, forcing us to return an HTTP 500, as we
can not detect that the error was due to that.
Instead, i now validate that the slug is indeed a valid UUID before
attempting to send it to the database, returning the correct HTTP error
code and avoiding useless calls to the database.
I based the validation function of Parse() from Google’s uuid package[0]
because this function is an order or magnitude faster in benchmarks:
goos: linux
goarch: amd64
pkg: dev.tandem.ws/tandem/numerus/pkg
cpu: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
BenchmarkValidUuid-4 36946050 29.37 ns/op
BenchmarkValidUuid_Re-4 3633169 306.70 ns/op
The regular expression used for the benchmark was:
var re = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
And the input parameter for both functions was the following valid UUID,
because most of the time the passed UUID will be valid:
"f47ac10b-58cc-0372-8567-0e02b2c3d479"
I did not use the uuid package, even though it is in Debian’s
repository, because i only need to check whether the value is valid,
not convert it to a byte array. As far as i know, that package can not
do that.
[0]: https://github.com/google/uuid
2023-07-17 09:46:11 +00:00
slug = conn . MustGetText ( r . Context ( ) , "" , "update quote set quote_status = $1 where slug = $2 returning slug" , form . QuoteStatus , slug )
2023-06-07 14:35:31 +00:00
if slug == "" {
http . NotFound ( w , r )
Return HTTP 404 instead of 500 for invalid UUID values in URL
Since most of PL/pgSQL functions accept a `uuid` domain, we get an error
if the value is not valid, forcing us to return an HTTP 500, as we
can not detect that the error was due to that.
Instead, i now validate that the slug is indeed a valid UUID before
attempting to send it to the database, returning the correct HTTP error
code and avoiding useless calls to the database.
I based the validation function of Parse() from Google’s uuid package[0]
because this function is an order or magnitude faster in benchmarks:
goos: linux
goarch: amd64
pkg: dev.tandem.ws/tandem/numerus/pkg
cpu: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
BenchmarkValidUuid-4 36946050 29.37 ns/op
BenchmarkValidUuid_Re-4 3633169 306.70 ns/op
The regular expression used for the benchmark was:
var re = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
And the input parameter for both functions was the following valid UUID,
because most of the time the passed UUID will be valid:
"f47ac10b-58cc-0372-8567-0e02b2c3d479"
I did not use the uuid package, even though it is in Debian’s
repository, because i only need to check whether the value is valid,
not convert it to a byte array. As far as i know, that package can not
do that.
[0]: https://github.com/google/uuid
2023-07-17 09:46:11 +00:00
return
2023-06-07 14:35:31 +00:00
}
htmxRedirect ( w , r , companyURI ( mustGetCompany ( r ) , "/quotes" ) )
} else {
if ! form . Validate ( ) {
if ! IsHTMxRequest ( r ) {
w . WriteHeader ( http . StatusUnprocessableEntity )
}
mustRenderEditQuoteForm ( w , r , slug , form )
return
}
Allow empty contact and payment method for quotes
I have to use a value to be used as “none” for payment method and
contact. In PL/pgSQL add_quote and edit_quote functions, that value is
NULL, while in forms it is the empty string. I can not simply pass the
empty string for either of these fields because PL/pgSQL expects
(nullable) integers, and "" is not a valid integer and is not NULL
either. A conversion is necessary.
Apparently, Go’s nil is not a valid representation for SQL’s NULL with
pgx, and had to use sql.NullString instead.
I also needed to coalesce contact’s VATIN and phone, because null values
can not be scanned to *string. I did not do that before because
`coalesce(vatin, '')` throws an error that '' is not a valid VATIN and
just left as is, wrongly expecting that pgx would do the job of leaving
the string blank for me. It does not.
Lastly, i can not blindly write Quotee’s tax details in the quote’s view
page, or we would see the (), characters for the empty address info.
2023-06-08 11:05:41 +00:00
slug = conn . MustGetText ( r . Context ( ) , "" , "select edit_quote($1, $2, $3, $4, $5, $6, $7, $8)" , slug , form . QuoteStatus , form . Customer . OrNull ( ) , form . TermsAndConditions , form . Notes , form . PaymentMethod . OrNull ( ) , form . Tags , EditedQuoteProductArray ( form . Products ) )
2023-06-07 14:35:31 +00:00
if slug == "" {
http . NotFound ( w , r )
return
}
htmxRedirect ( w , r , companyURI ( company , "/quotes/" + slug ) )
}
}
func ServeEditQuote ( w http . ResponseWriter , r * http . Request , params httprouter . Params ) {
conn := getConn ( r )
company := mustGetCompany ( r )
slug := params [ 0 ] . Value
Return HTTP 404 instead of 500 for invalid UUID values in URL
Since most of PL/pgSQL functions accept a `uuid` domain, we get an error
if the value is not valid, forcing us to return an HTTP 500, as we
can not detect that the error was due to that.
Instead, i now validate that the slug is indeed a valid UUID before
attempting to send it to the database, returning the correct HTTP error
code and avoiding useless calls to the database.
I based the validation function of Parse() from Google’s uuid package[0]
because this function is an order or magnitude faster in benchmarks:
goos: linux
goarch: amd64
pkg: dev.tandem.ws/tandem/numerus/pkg
cpu: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
BenchmarkValidUuid-4 36946050 29.37 ns/op
BenchmarkValidUuid_Re-4 3633169 306.70 ns/op
The regular expression used for the benchmark was:
var re = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
And the input parameter for both functions was the following valid UUID,
because most of the time the passed UUID will be valid:
"f47ac10b-58cc-0372-8567-0e02b2c3d479"
I did not use the uuid package, even though it is in Debian’s
repository, because i only need to check whether the value is valid,
not convert it to a byte array. As far as i know, that package can not
do that.
[0]: https://github.com/google/uuid
2023-07-17 09:46:11 +00:00
if ! ValidUuid ( slug ) {
http . NotFound ( w , r )
return
}
2023-06-07 14:35:31 +00:00
locale := getLocale ( r )
form := newQuoteForm ( r . Context ( ) , conn , locale , company )
if ! form . MustFillFromDatabase ( r . Context ( ) , conn , slug ) {
http . NotFound ( w , r )
return
}
w . WriteHeader ( http . StatusOK )
mustRenderEditQuoteForm ( w , r , slug , form )
}
type editQuotePage struct {
* newQuotePage
Slug string
Number string
}
func newEditQuotePage ( slug string , form * quoteForm , r * http . Request ) * editQuotePage {
return & editQuotePage {
newNewQuotePage ( form , r ) ,
slug ,
form . Number ,
}
}
func mustRenderEditQuoteForm ( w http . ResponseWriter , r * http . Request , slug string , form * quoteForm ) {
page := newEditQuotePage ( slug , form , r )
mustRenderMainTemplate ( w , r , "quotes/edit.gohtml" , page )
}
func HandleEditQuoteAction ( w http . ResponseWriter , r * http . Request , params httprouter . Params ) {
slug := params [ 0 ] . Value
Return HTTP 404 instead of 500 for invalid UUID values in URL
Since most of PL/pgSQL functions accept a `uuid` domain, we get an error
if the value is not valid, forcing us to return an HTTP 500, as we
can not detect that the error was due to that.
Instead, i now validate that the slug is indeed a valid UUID before
attempting to send it to the database, returning the correct HTTP error
code and avoiding useless calls to the database.
I based the validation function of Parse() from Google’s uuid package[0]
because this function is an order or magnitude faster in benchmarks:
goos: linux
goarch: amd64
pkg: dev.tandem.ws/tandem/numerus/pkg
cpu: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
BenchmarkValidUuid-4 36946050 29.37 ns/op
BenchmarkValidUuid_Re-4 3633169 306.70 ns/op
The regular expression used for the benchmark was:
var re = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
And the input parameter for both functions was the following valid UUID,
because most of the time the passed UUID will be valid:
"f47ac10b-58cc-0372-8567-0e02b2c3d479"
I did not use the uuid package, even though it is in Debian’s
repository, because i only need to check whether the value is valid,
not convert it to a byte array. As far as i know, that package can not
do that.
[0]: https://github.com/google/uuid
2023-07-17 09:46:11 +00:00
if ! ValidUuid ( slug ) {
http . NotFound ( w , r )
return
}
2023-06-07 14:35:31 +00:00
actionUri := fmt . Sprintf ( "/quotes/%s/edit" , slug )
handleQuoteAction ( w , r , actionUri , func ( w http . ResponseWriter , r * http . Request , form * quoteForm ) {
conn := getConn ( r )
form . Number = conn . MustGetText ( r . Context ( ) , "" , "select quote_number from quote where slug = $1" , slug )
mustRenderEditQuoteForm ( w , r , slug , form )
} )
}
type renderQuoteFormFunc func ( w http . ResponseWriter , r * http . Request , form * quoteForm )
func handleQuoteAction ( w http . ResponseWriter , r * http . Request , action string , renderForm renderQuoteFormFunc ) {
locale := getLocale ( r )
conn := getConn ( r )
company := mustGetCompany ( r )
form := newQuoteForm ( r . Context ( ) , conn , locale , company )
if err := form . Parse ( r ) ; err != nil {
http . Error ( w , err . Error ( ) , http . StatusBadRequest )
return
}
if err := verifyCsrfTokenValid ( r ) ; err != nil {
http . Error ( w , err . Error ( ) , http . StatusForbidden )
return
}
actionField := r . Form . Get ( "action" )
switch actionField {
case "update" :
form . Update ( )
w . WriteHeader ( http . StatusOK )
renderForm ( w , r , form )
case "select-products" :
w . WriteHeader ( http . StatusOK )
mustRenderNewQuoteProductsForm ( w , r , action , form )
case "add-products" :
form . AddProducts ( r . Context ( ) , conn , r . Form [ "slug" ] )
w . WriteHeader ( http . StatusOK )
renderForm ( w , r , form )
case "restore-product" :
restoredProduct := newQuoteProductForm ( 0 , company , locale , mustGetTaxOptions ( r . Context ( ) , conn , company ) )
restoredProduct . RenameWithSuffix ( removedProductSuffix )
if err := restoredProduct . Parse ( r ) ; err != nil {
panic ( err )
}
form . InsertProduct ( restoredProduct )
form . Update ( )
w . WriteHeader ( http . StatusOK )
renderForm ( w , r , form )
default :
prefix := "remove-product."
if strings . HasPrefix ( actionField , prefix ) {
index , err := strconv . Atoi ( actionField [ len ( prefix ) : ] )
if err != nil {
http . Error ( w , gettext ( "Invalid action" , locale ) , http . StatusBadRequest )
} else {
form . RemoveProduct ( index )
form . Update ( )
w . WriteHeader ( http . StatusOK )
renderForm ( w , r , form )
}
} else {
http . Error ( w , gettext ( "Invalid action" , locale ) , http . StatusBadRequest )
}
}
}
func ServeEditQuoteTags ( w http . ResponseWriter , r * http . Request , params httprouter . Params ) {
2024-08-15 02:18:18 +00:00
serveTagsEditForm ( w , r , params , "/quotes/" , "select tags from quote where slug = $1" )
2023-06-07 14:35:31 +00:00
}
func HandleUpdateQuoteTags ( w http . ResponseWriter , r * http . Request , params httprouter . Params ) {
2024-08-15 02:18:18 +00:00
handleUpdateTags ( w , r , params , "/quotes/" , "update quote set tags = $1 where slug = $2 returning slug" )
2023-06-07 14:35:31 +00:00
}