Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

291 proposal merge commercetools tax category rate into commercetools tax category #477

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changes/unreleased/Added-20240202-162014.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Added
body: Added tax_category_v2 resource to improve tax rate stability
time: 2024-02-02T16:20:14.042422395+01:00
19 changes: 19 additions & 0 deletions .github/actions/has-change-file/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Has Change File
description: Check if a new change file is set

runs:
using: "composite"
steps:
- uses: dorny/paths-filter@v3
id: changes
with:
filters: |
changes:
- '.changes/unreleased/**'

- name: Check changes
if: steps.changes.outputs.changes == 'false'
shell: bash
run: |
echo "No new change file set in .changes/unreleased"
exit 1
3 changes: 3 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jobs:
steps:
- uses: actions/checkout@v3

- name: Has changes file
uses: ./.github/actions/has-change-file

- uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.3.7
Expand Down
3 changes: 3 additions & 0 deletions commercetools/resource_tax_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ func resourceTaxCategory() *schema.Resource {
return &schema.Resource{
Description: "Tax Categories define how products are to be taxed in different countries.\n\n" +
"See also the [Tax Category API Documentation](https://docs.commercetools.com/api/projects/taxCategories)",

DeprecationMessage: "This resource is deprecated and will be removed in the next major release. " +
"Please use the commercetools_tax_category_v2 resource instead.",
CreateContext: resourceTaxCategoryCreate,
ReadContext: resourceTaxCategoryRead,
UpdateContext: resourceTaxCategoryUpdate,
Expand Down
2 changes: 2 additions & 0 deletions commercetools/resource_tax_category_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func resourceTaxCategoryRate() *schema.Resource {
Importer: &schema.ResourceImporter{
StateContext: resourceTaxCategoryRateImportState,
},
DeprecationMessage: "This resource is deprecated and will be removed in the next major release. " +
"Please use the tax_rate attributes on commercetools_tax_category resource instead.",
Schema: map[string]*schema.Schema{
"tax_category_id": {
Type: schema.TypeString,
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/tax_category.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ description: |-

# commercetools_tax_category (Resource)

> **DEPRECATED**: The resource has been deprecated. Please use [commercetools_tax_category_v2](./tax_category_v2.md) instead.

Tax Categories define how products are to be taxed in different countries.

See also the [Tax Category API Documentation](https://docs.commercetools.com/api/projects/taxCategories)
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/tax_category_rate.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ description: |-

# commercetools_tax_category_rate (Resource)

> **DEPRECATED**: The resource has been deprecated. Please use [commercetools_tax_category_v2](./tax_category_v2.md) instead.

Tax rate for Tax Category.

See also [Tax Rate API Documentation](https://docs.commercetools.com/api/projects/taxCategories#taxrate)
Expand Down
62 changes: 62 additions & 0 deletions docs/resources/tax_category_v2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "commercetools_tax_category_v2 Resource - terraform-provider-commercetools"
subcategory: ""
description: |-
Tax Categories define how products are to be taxed in different countries.
See also the Tax Category API Documentation https://docs.commercetools.com/api/projects/taxCategories.
---

# commercetools_tax_category_v2 (Resource)

Tax Categories define how products are to be taxed in different countries.

See also the [Tax Category API Documentation](https://docs.commercetools.com/api/projects/taxCategories).



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Name of the TaxCategory.

### Optional

- `description` (String) Description of the TaxCategory.
- `key` (String) User-defined unique identifier of the TaxCategory
- `tax_rate` (Block List) Attributes with unique values. (see [below for nested schema](#nestedblock--tax_rate))

### Read-Only

- `id` (String) Unique identifier of the TaxCategory.
- `version` (Number) Current version of the TaxCategory.

<a id="nestedblock--tax_rate"></a>
### Nested Schema for `tax_rate`

Required:

- `amount` (Number) Number Percentage in the range of [0..1]. The sum of the amounts of all subRates, if there are any
- `country` (String) A two-digit country code as per [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)
- `included_in_price` (Boolean) If true, tax is included in Embedded Prices or Standalone Prices, and the taxedPrice is present on LineItems. In this case, the totalNet price on TaxedPrice includes the TaxRate.
- `name` (String) Name of the TaxRate.

Optional:

- `key` (String) User-defined unique identifier of the TaxCategory
- `state` (String) The state in the country
- `sub_rate` (Block List) For countries (for example the US) where the total tax is a combination of multiple taxes (for example state and local taxes) (see [below for nested schema](#nestedblock--tax_rate--sub_rate))

Read-Only:

- `id` (String) Present if the TaxRate is part of a TaxCategory. Absent for external TaxRates in LineItem, CustomLineItem, and ShippingInfo.

<a id="nestedblock--tax_rate--sub_rate"></a>
### Nested Schema for `tax_rate.sub_rate`

Required:

- `amount` (Number) Number Percentage in the range of [0..1]. The sum of the amounts of all subRates, if there are any
- `name` (String) Name of the SubRate.
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"github.com/labd/terraform-provider-commercetools/internal/resources/tax_category_v2"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -196,5 +197,6 @@ func (p *ctProvider) Resources(_ context.Context) []func() resource.Resource {
attribute_group.NewResource,
associate_role.NewResource,
product_selection.NewResource,
tax_category_v2.NewResource,
}
}
174 changes: 174 additions & 0 deletions internal/resources/tax_category_v2/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package tax_category_v2

import (
"fmt"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/labd/commercetools-go-sdk/platform"
"reflect"
)

type TaxCategory struct {
ID types.String `tfsdk:"id"`
Version types.Int64 `tfsdk:"version"`
Key types.String `tfsdk:"key"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
TaxRates []TaxRate `tfsdk:"tax_rates"`
}

type TaxRate struct {
ID types.String `tfsdk:"id"`
Key types.String `tfsdk:"key"`
Name types.String `tfsdk:"name"`
Amount types.Float64 `tfsdk:"amount"`
IncludedInPrice types.Bool `tfsdk:"included_in_price"`
Country types.String `tfsdk:"country"`
State types.String `tfsdk:"state"`
SubRates []SubRate `tfsdk:"sub_rates"`
}

type SubRate struct {
Name types.String `tfsdk:"name"`
Amount types.Float64 `tfsdk:"amount"`
}

func (tr TaxRate) draft() platform.TaxRateDraft {
var subRateDrafts = make([]platform.SubRate, 0, len(tr.SubRates))

for _, subRate := range tr.SubRates {
subRateDrafts = append(subRateDrafts, platform.SubRate{
Name: subRate.Name.ValueString(),
Amount: subRate.Amount.ValueFloat64(),
})
}

Check warning on line 43 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L35-L43

Added lines #L35 - L43 were not covered by tests

return platform.TaxRateDraft{
Name: tr.Name.ValueString(),
Amount: tr.Amount.ValueFloat64Pointer(),
IncludedInPrice: tr.IncludedInPrice.ValueBool(),
Country: tr.Country.ValueString(),
State: tr.State.ValueStringPointer(),
SubRates: subRateDrafts,
}

Check warning on line 52 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L45-L52

Added lines #L45 - L52 were not covered by tests
}

func (tc TaxCategory) draft() platform.TaxCategoryDraft {
var taxRateDrafts = make([]platform.TaxRateDraft, 0, len(tc.TaxRates))

for _, taxRate := range tc.TaxRates {
taxRateDrafts = append(taxRateDrafts, taxRate.draft())
}

Check warning on line 60 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L55-L60

Added lines #L55 - L60 were not covered by tests

return platform.TaxCategoryDraft{
Name: tc.Name.ValueString(),
Key: tc.Key.ValueStringPointer(),
Description: tc.Description.ValueStringPointer(),
Rates: taxRateDrafts,
}

Check warning on line 67 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L62-L67

Added lines #L62 - L67 were not covered by tests
}

func TaxCategoryFromNative(tc *platform.TaxCategory) (TaxCategory, error) {
var rates = make([]TaxRate, 0, len(tc.Rates))
for _, taxRate := range tc.Rates {
var idPtr = taxRate.ID
if idPtr == nil {
return TaxCategory{}, fmt.Errorf("tax rate ID is nil")
}

Check warning on line 76 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L70-L76

Added lines #L70 - L76 were not covered by tests

var subRates = make([]SubRate, 0, len(taxRate.SubRates))
for _, subRate := range taxRate.SubRates {
var subRate = SubRate{
Name: types.StringValue(subRate.Name),
Amount: types.Float64Value(subRate.Amount),
}
subRates = append(subRates, subRate)
}

Check warning on line 85 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L78-L85

Added lines #L78 - L85 were not covered by tests

var rate = TaxRate{
ID: types.StringValue(*idPtr),
Key: types.StringPointerValue(taxRate.Key),
Name: types.StringValue(taxRate.Name),
Amount: types.Float64Value(taxRate.Amount),
IncludedInPrice: types.BoolValue(taxRate.IncludedInPrice),
Country: types.StringValue(taxRate.Country),
State: types.StringPointerValue(taxRate.State),
SubRates: subRates,
}
rates = append(rates, rate)

Check warning on line 97 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L87-L97

Added lines #L87 - L97 were not covered by tests
}

return TaxCategory{
ID: types.StringValue(tc.ID),
Version: types.Int64Value(int64(tc.Version)),
Key: types.StringPointerValue(tc.Key),
Name: types.StringValue(tc.Name),
Description: types.StringPointerValue(tc.Description),
TaxRates: rates,
}, nil

Check warning on line 107 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L100-L107

Added lines #L100 - L107 were not covered by tests
}

func (tc TaxCategory) updateActions(plan TaxCategory) platform.TaxCategoryUpdate {
result := platform.TaxCategoryUpdate{
Version: int(tc.Version.ValueInt64()),
Actions: []platform.TaxCategoryUpdateAction{},
}

// setKey
if !tc.Key.Equal(plan.Key) {
var newKey *string
if !plan.Key.IsNull() && !plan.Key.IsUnknown() {
newKey = plan.Key.ValueStringPointer()
}

Check warning on line 121 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L110-L121

Added lines #L110 - L121 were not covered by tests

result.Actions = append(
result.Actions,
platform.TaxCategorySetKeyAction{Key: newKey},
)

Check warning on line 126 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L123-L126

Added lines #L123 - L126 were not covered by tests
}

// changeName
if !tc.Name.Equal(plan.Name) {
var newName string
if !plan.Name.IsNull() && !plan.Name.IsUnknown() {
newName = plan.Name.ValueString()
}

Check warning on line 134 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L130-L134

Added lines #L130 - L134 were not covered by tests

result.Actions = append(
result.Actions,
platform.TaxCategoryChangeNameAction{Name: newName},
)

Check warning on line 139 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L136-L139

Added lines #L136 - L139 were not covered by tests
}

// setDescription
if !tc.Description.Equal(plan.Description) {
var newDescription *string
if !plan.Description.IsNull() && !plan.Description.IsUnknown() {
newDescription = plan.Description.ValueStringPointer()
}

Check warning on line 147 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L143-L147

Added lines #L143 - L147 were not covered by tests

result.Actions = append(
result.Actions,
platform.TaxCategorySetDescriptionAction{Description: newDescription},
)

Check warning on line 152 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L149-L152

Added lines #L149 - L152 were not covered by tests
}

// If a change occurred in tax rates we remove all the tax rates and re-add them.
// This is because a change manual change in tax rate through the API or merchant
// center re-creates the tax rate so the ID cannot be trusted
if !reflect.DeepEqual(tc.TaxRates, plan.TaxRates) {
for _, tr := range tc.TaxRates {
result.Actions = append(
result.Actions,
platform.TaxCategoryRemoveTaxRateAction{TaxRateId: tr.ID.ValueStringPointer()},
)
}

Check warning on line 164 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L158-L164

Added lines #L158 - L164 were not covered by tests

for _, dtr := range plan.TaxRates {
result.Actions = append(
result.Actions,
platform.TaxCategoryAddTaxRateAction{TaxRate: dtr.draft()})
}

Check warning on line 170 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L166-L170

Added lines #L166 - L170 were not covered by tests
}

return result

Check warning on line 173 in internal/resources/tax_category_v2/model.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/tax_category_v2/model.go#L173

Added line #L173 was not covered by tests
}
Loading
Loading