From 3aec74d679e03e1d54c1634485e3935ec9dd73b5 Mon Sep 17 00:00:00 2001 From: Vincent Pochet Date: Thu, 2 Nov 2023 15:14:19 +0100 Subject: [PATCH] feat(credit-note): Add estimate route before creation (#140) --- credit_note.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/credit_note.go b/credit_note.go index c0dd554..ee9eb4d 100644 --- a/credit_note.go +++ b/credit_note.go @@ -46,6 +46,10 @@ type CreditNoteResult struct { Meta Metadata `json:"meta,omitempty"` } +type CreditNoteEstimatedResult struct { + CreditNoteEstimated *CreditNoteEstimated `json:"credit_note_estimated"` +} + type CreditListInput struct { PerPage int `json:"per_page,omitempty,string"` Page int `json:"page,omitempty,string"` @@ -92,7 +96,7 @@ type CreditNote struct { TaxesAmountCents int `json:"taxes_amount_cents,omitempty"` TaxesRate float32 `json:"taxes_rate,omitempty"` SubTotalExcludingTaxesAmountCents int `json:"sub_total_excluding_taxes_amount_cents,omitempty"` - CouponsAdjustementAmountCents int `json:"coupons_adjustement_amount_cents,omitempty"` + CouponsAdjustmentAmountCents int `json:"coupons_adjustment_amount_cents,omitempty"` FileURL string `json:"file_url,omitempty"` @@ -112,6 +116,38 @@ type CreditNote struct { SubTotalVatExcludedAmountCurrency Currency `json:"sub_total_vat_excluded_amount_currency,omitempty"` } +type CreditNoteEstimated struct { + LagoInvoiceID uuid.UUID `json:"lago_invoice_id,omitempty"` + InvoiceNumber string `json:"invoice_number,omitempty"` + + Currency Currency `json:"currency,omitempty"` + MaxCreditableAmountCents int `json:"max_creditable_amount_cents,omitempty"` + MaxRefundableAmountCents int `json:"max_refundable_amount_cents,omitempty"` + TaxesAmountCents int `json:"taxes_amount_cents,omitempty"` + TaxesRate float32 `json:"taxes_rate,omitempty"` + SubTotalExcludingTaxesAmountCents int `json:"sub_total_excluding_taxes_amount_cents,omitempty"` + CouponsAdjustmentAmountCents int `json:"coupons_adjustment_amount_cents,omitempty"` + + Items []CreditNoteEstimatedItem `json:"items,omitempty"` + + AppliedTaxes []CreditNoteEstimatedAppliedTax `json:"applied_taxes,omitempty"` +} + +type CreditNoteEstimatedItem struct { + AmountCents int `json:"amount_cents,omitempty"` + LagoFeeID uuid.UUID `json:"lago_fee_id,omitempty"` +} + +type CreditNoteEstimatedAppliedTax struct { + LagoTaxId uuid.UUID `json:"lago_tax_id,omitempty"` + TaxName string `json:"tax_name,omitempty"` + TaxCode string `json:"tax_code,omitempty"` + TaxRate float32 `json:"tax_rate,omitempty"` + TaxDescription string `json:"tax_description,omitempty"` + AmountCents int `json:"amount_cents,omitempty"` + AmountCurrency Currency `json:"amount_currency,omitempty"` +} + type CreditNoteItemInput struct { LagoFeeID uuid.UUID `json:"fee_id,omitempty"` AmountCents int `json:"amount_cents,omitempty"` @@ -134,6 +170,15 @@ type CreditNoteUpdateParams struct { CreditNote *CreditNoteUpdateInput `json:"credit_note"` } +type CreditNoteEstimateInput struct { + LagoInvoiceID uuid.UUID `json:"invoice_id,omitempty"` + Items []CreditNoteItemInput `json:"items,omitempty"` +} + +type CreditNoteEstimateParams struct { + CreditNote *CreditNoteEstimateInput `json:"credit_note"` +} + func (c *Client) CreditNote() *CreditNoteRequest { return &CreditNoteRequest{ client: c, @@ -284,3 +329,27 @@ func (cr *CreditNoteRequest) Void(ctx context.Context, creditNoteID string) (*Cr return creditNoteResult.CreditNote, nil } + +func (cr *CreditNoteRequest) Estimate(ctx context.Context, creditNoteEstimateInput *CreditNoteEstimateInput) (*CreditNoteEstimated, *Error) { + creditNoteEstimateParams := &CreditNoteEstimateParams{ + CreditNote: creditNoteEstimateInput, + } + + clientRequest := &ClientRequest{ + Path: "credit_notes/estimate", + Result: &CreditNoteEstimatedResult{}, + Body: creditNoteEstimateParams, + } + + result, err := cr.client.Post(ctx, clientRequest) + if err != nil { + return nil, err + } + + creditNoteEstimatedResult, ok := result.(*CreditNoteEstimatedResult) + if !ok { + return nil, &ErrorTypeAssert + } + + return creditNoteEstimatedResult.CreditNoteEstimated, nil +}