Skip to content

Commit

Permalink
Moving to simpler package names, adding mx region specific stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed May 3, 2024
1 parent 0c4f302 commit 4901995
Show file tree
Hide file tree
Showing 54 changed files with 2,407 additions and 296 deletions.
2 changes: 1 addition & 1 deletion cmd/gobl.html/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func run() error {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()

if err := ctxi18n.Load(locales.Content); err != nil {
if err := ctxi18n.LoadWithDefault(locales.Content, "en"); err != nil {
return fmt.Errorf("loading locales: %w", err)
}

Expand Down
56 changes: 38 additions & 18 deletions cmd/gobl.html/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
"strings"
"time"

"github.com/invopop/ctxi18n"
"github.com/invopop/ctxi18n/i18n"
"github.com/invopop/gobl"
goblhtml "github.com/invopop/gobl.html"
"github.com/invopop/gobl.html/pkg/pdf"
"github.com/invopop/gobl/org"
"github.com/labstack/echo/v4"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -83,37 +84,56 @@ func (s *serveOpts) runE(cmd *cobra.Command, args []string) error {
return startErr
}

func (s *serveOpts) render(c echo.Context, env *gobl.Envelope) ([]byte, error) {
func (s *serveOpts) render(c echo.Context, req *options, env *gobl.Envelope) ([]byte, error) {
ctx := c.Request().Context()
var err error

locale := c.QueryParam("locale")
if locale == "" {
locale = "en"
// Prepare the request options
opts := make([]goblhtml.Option, 0)
if req.DateFormat != "" {
opts = append(opts, goblhtml.WithCalFormatter(req.DateFormat, "", time.UTC))
}
opts = append(opts, goblhtml.WithLocale(req.Locale))

fmt.Printf("LOCALE: %v\n", locale)

// Set the locale to English to start with
ctx := c.Request().Context()
ctx, err = ctxi18n.WithLocale(ctx, locale)
if err != nil {
return nil, fmt.Errorf("setting locale: %w", err)
// Add some of the extras to the output
if req.LogoURL != "" {
logo := &org.Image{
URL: req.LogoURL,
Height: req.LogoHeight,
}
opts = append(opts, goblhtml.WithLogo(logo))
}
if req.Notes != "" {
opts = append(opts, goblhtml.WithNotes(req.Notes))
}

out, err := goblhtml.Render(ctx, env)
out, err := goblhtml.Render(ctx, env, opts...)
if err != nil {
return nil, fmt.Errorf("generating html: %w", err)
}

return out, nil
}

type options struct {
Filename string `param:"filename"`
Locale i18n.Code `query:"locale"`
DateFormat string `query:"date_format"`
LogoURL string `query:"logo_url"`
LogoHeight int32 `query:"logo_height"`
Notes string `query:"notes"`
}

func (s *serveOpts) generate(c echo.Context) error {
filename := c.Param("filename")
ext := filepath.Ext(filename)
filename = strings.TrimSuffix(filename, ext) + ".json"
req := new(options)
if err := c.Bind(req); err != nil {
return fmt.Errorf("binding options: %w", err)
}

ext := filepath.Ext(req.Filename)
req.Filename = strings.TrimSuffix(req.Filename, ext) + ".json"

ed, err := os.ReadFile(filepath.Join("./examples", filename))
ed, err := os.ReadFile(filepath.Join("./examples", req.Filename))
if err != nil {
return fmt.Errorf("loading file: %w", err)
}
Expand All @@ -123,7 +143,7 @@ func (s *serveOpts) generate(c echo.Context) error {
return fmt.Errorf("unmarshalling file: %w", err)
}

data, err := s.render(c, env)
data, err := s.render(c, req, env)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package invoice

import (
"github.com/invopop/gobl/org"
"github.com/invopop/gobl.html/components/organizing"
"github.com/invopop/gobl/bill"
"github.com/invopop/gobl.html/components/org"
"github.com/invopop/gobl.html/components/t"
)

templ customer(party *org.Party) {
templ customer(inv *bill.Invoice) {
<section class="customer">
<h2>
@t.T("billing.invoice.customer.title")
</h2>
@organizing.Party(party)
@org.Party(inv.Customer)
</section>
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/invopop/gobl.html/components/regimes/es"
"github.com/invopop/gobl.html/components/regimes/co"
"github.com/invopop/gobl.html/components/regimes/mx"
)

// Invoice renders a complete GOBL bill.Invoice object.
Expand Down Expand Up @@ -100,7 +101,6 @@ templ Invoice(env *gobl.Envelope, inv *bill.Invoice) {
order: 5;
padding-top: 6mm;
padding-bottom: 0mm;

}
section.lines h2 {
display: none;
Expand Down Expand Up @@ -139,6 +139,12 @@ templ Invoice(env *gobl.Envelope, inv *bill.Invoice) {
section.lines .label {
font-weight: 500;
}
section.lines .description .extensions {
display: block;
}
section.lines .description .extensions .label {
font-weight: 500;
}
div.totals {
order: 8;
display: flex;
Expand Down Expand Up @@ -263,13 +269,13 @@ templ Invoice(env *gobl.Envelope, inv *bill.Invoice) {
section.notes .note {
margin-bottom: 3mm;
}
section.extensions {
.extensions section {
break-inside: avoid;
padding-top: 6mm;
padding-bottom: 6mm;
border-bottom: 0.5px solid #E5E7EB;
}
section.extensions:empty {
.extensions:empty {
display: none;
}
.org-party .name {
Expand All @@ -287,9 +293,9 @@ templ Invoice(env *gobl.Envelope, inv *bill.Invoice) {
@summary(inv)
</div>
<div class="contacts">
@supplier(inv.Supplier)
@supplier(inv)
if inv.Customer != nil {
@customer(inv.Customer)
@customer(inv)
}
</div>
</header>
Expand All @@ -300,11 +306,12 @@ templ Invoice(env *gobl.Envelope, inv *bill.Invoice) {
</div>
@payment(inv)
@notes(inv)
<section class="extensions">
<div class="extensions">
// Region specific information
@es.TicketBAIQR(env)
@co.DIANQR(env, inv)
</section>
@mx.CFDI(env, inv)
</div>
</article>
}

Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ func prepareLineSupport(inv *bill.Invoice) *lineSupport {
ls.units = true
}
for _, combo := range l.Taxes {
cats = addCategory(cats, r.Category(combo.Category))
cat := r.Category(combo.Category)
if cat != nil {
cats = addCategory(cats, cat)
}
}
}
for _, row := range inv.Discounts {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/invopop/gobl/bill"
"github.com/invopop/gobl.html/components/t"
"github.com/invopop/gobl.html/components/regimes/mx"
)

templ lines(inv *bill.Invoice) {
Expand Down Expand Up @@ -45,7 +46,7 @@ templ linesWithSupport(inv *bill.Invoice, ls *lineSupport) {
</th>
for _, cat := range ls.categories {
<th class="tax">
{ cat.Name.String() }
{ cat.Name.In(t.Lang(ctx)) }
</th>
}
if ls.discounts {
Expand Down Expand Up @@ -98,6 +99,7 @@ templ line(_ *bill.Invoice, l *bill.Line, ls *lineSupport) {
<br/>
<small>{ l.Item.Description }</small>
}
@mx.LineExtensions(l)
</td>
<td class="quantity">
@t.L(l.Quantity)
Expand All @@ -117,7 +119,11 @@ templ line(_ *bill.Invoice, l *bill.Line, ls *lineSupport) {
for _, cat := range ls.categories {
<td class="tax">
if combo := l.Taxes.Get(cat.Code); combo != nil {
@t.L(*combo.Percent)
if combo.Percent != nil {
@t.L(*combo.Percent)
} else {
&mdash;
}
} else {
<!-- empty -->
}
Expand Down
Loading

0 comments on commit 4901995

Please sign in to comment.