Skip to content

Commit

Permalink
WIP (missing files)
Browse files Browse the repository at this point in the history
  • Loading branch information
cavalle committed Jun 18, 2024
1 parent 76934ee commit c21947b
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
66 changes: 66 additions & 0 deletions components/regimes/pt/at.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package pt

import (
"github.com/invopop/gobl"
"github.com/invopop/gobl/regimes/pt"
)

templ ATQR(env *gobl.Envelope) {
if atcud := atATCUD(env); atcud != "" {
if qr := atQR(env); qr != "" {
@generateATQR(atcud, qr)
}
}
}

templ generateATQR(atcud, qr string) {
<style type="text/css">
.at {
break-inside: avoid;
text-align: center;
padding: 3mm;
border: 1px solid #E5E7EB;
width: 6cm;
}
.at .label {
font-family: monospace;
font-size: 7pt;
text-align: center;
margin-bottom: 2mm;
}
.at img {
width: 30mm;
height: 30mm;
}
</style>
<section>
<div class="at">
<div class="label">
ATCUD: { atcud }
</div>
<div class="qr">
<img src={ generateQR(qr) }/>
</div>
</div>
</section>
}

func atATCUD(env *gobl.Envelope) string {
for _, stamp := range env.Head.Stamps {
switch stamp.Provider {
case pt.StampProviderATATCUD:
return stamp.Value
}
}
return ""
}

func atQR(env *gobl.Envelope) string {
for _, stamp := range env.Head.Stamps {
switch stamp.Provider {
case pt.StampProviderATQR:
return stamp.Value
}
}
return ""
}
109 changes: 109 additions & 0 deletions components/regimes/pt/at_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions components/regimes/pt/pt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package pt

Check warning on line 1 in components/regimes/pt/pt.go

View workflow job for this annotation

GitHub Actions / golangci-lint

package-comments: should have a package comment (revive)

import (
"bytes"
"encoding/base64"

go_qr "github.com/piglig/go-qr"
)

// Parameters required by the AT for the QR code generator
const (
// Minimum version
atQRMinVer = 9

// Error correction level
atQRCorLvl = go_qr.Medium
)

// generateQR implements a custom QR code generator that complies with the AT spec.
func generateQR(qrval string) string {
// The AT spec requires the QR code to be encoded in binary/bytes mode
seg, err := go_qr.MakeBytes([]byte(qrval))
if err != nil {
return ""
}

segs := []*go_qr.QrSegment{seg}

// Encode according to the AT specs
qr, err := go_qr.EncodeSegments(segs, atQRCorLvl, atQRMinVer, go_qr.MaxVersion, -1, false)
if err != nil {
return ""
}

conf := go_qr.NewQrCodeImgConfig(10, 4)

buf := new(bytes.Buffer)
if err := qr.WriteAsSVG(conf, buf, "#FFFFFF", "#000000"); err != nil {
return ""
}

str := base64.StdEncoding.EncodeToString(buf.Bytes())
return "data:image/svg+xml;base64," + str
}

0 comments on commit c21947b

Please sign in to comment.