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

Tax rates with dates #14338

Merged
merged 9 commits into from
Oct 10, 2022
Merged

Tax rates with dates #14338

merged 9 commits into from
Oct 10, 2022

Conversation

everwhatever
Copy link
Contributor

@everwhatever everwhatever commented Sep 21, 2022

Q A
Branch? 1.12
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets jira
License MIT

@everwhatever everwhatever requested a review from a team as a code owner September 21, 2022 09:46
@probot-autolabeler probot-autolabeler bot added the Admin AdminBundle related issues and PRs. label Sep 21, 2022
@Zales0123 Zales0123 added the Feature New feature proposals. label Sep 22, 2022
@coldic3
Copy link
Contributor

coldic3 commented Sep 23, 2022

Could you also remove composer.phar and env.local?

@vvasiloi
Copy link
Contributor

Can this be reduced to a custom query?
Are repository methods part of the BC promise?

@vvasiloi
Copy link
Contributor

Can this be reduced to a custom query? Are repository methods part of the BC promise?

There isn't a custom repository for tax rates, so there won't be a BC break.
The repository could look like this:

class TaxRateRepository extends EntityRepository implements TaxRateRepositoryInterface
{
    public function findOneByCategoryAndDate(TaxCategoryInterface $taxCategory, ?\DateTimeInterface $date = null): ?TaxRateInterface {
        if (null === $date) {
            $date = new \DateTime();
        }

        $expr = $this->_em->getExpressionBuilder();

        return $this->createQueryBuilder('o')
            ->where(
                $expr->andX(
                    $expr->eq('o.category', ':category'),
                    $expr->orX(
                        $expr->isNull('o.startDate'),
                        $expr->lte('o.startDate', ':date')
                    ),
                    $expr->orX(
                        $expr->isNull('o.endDate'),
                        $expr->gte('o.endDate', ':date')
                    ),
                )
            )
            ->setParameter('category', $taxCategory)
            ->setParameter('date', $date)
            ->getQuery()
            ->getOneOrNullResult();
    }
}

However, a way to add the zone in the core bundle is needed, which complicates things a bit. 🤔
We could add a child repo in the core bundle with another method that also takes in the zone and extracts the common query builder/expression, but the fact that the resolver accepts a criteria array that's passed directly to the findBy/findOneBy makes things weird.

If we keep the date filter logic in PHP, then I suggest transforming the TaxRateDateChecker into an "eligibility checker" (which is common concept in Sylius) that will take a single tax rate and return true if it fulfils the conditions and false otherwise.
Basically, the checker will contain the logic that's now in TaxRateDateChecker::isInDate, which now is a public method that's not part of the abstraction.
Then the resolver will iterate over the tax rates returned from the repository and return the first one that's eligible.

Copy link
Contributor

@NoResponseMate NoResponseMate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing behat tests for tax rate form validation

@probot-autolabeler probot-autolabeler bot added the Maintenance CI configurations, READMEs, releases, etc. label Sep 29, 2022
@vvasiloi
Copy link
Contributor

vvasiloi commented Oct 4, 2022

🤔 maybe we can avoid the deprecation by decorating the original resolver instead of modifying it? WDYT?

Edit:

  • Add an EligibleTaxRateResolver next to the current TaxRateResolver in the taxation component
  • Add the decoration to the taxation bundle

@Zales0123
Copy link
Member

I believe that the deprecation is not a bad option here - of course, it can cause discomfort for the service user, but on the other hand, we transparently inform about important changes in tax rates resolving logic 🚀 Moreover, as the interface allows to return only one tax rate, we could not use the result of the base resolver to filter the ineligible tax rates, as we need an array of them 🖖

@vvasiloi
Copy link
Contributor

vvasiloi commented Oct 5, 2022

@Zales0123 my idea was to completely ignore the output from the current resolver, as it remains only for compatibility for those using the service somehow else. Now that I said it out loud it doesn't sound that good anymore.

@GSadee GSadee force-pushed the tax-rates-with-dates branch 3 times, most recently from 849cc85 to 5e6c658 Compare October 6, 2022 09:58
@@ -39,7 +39,13 @@

<service id="sylius.tax_rate_resolver" class="Sylius\Component\Taxation\Resolver\TaxRateResolver">
<argument type="service" id="sylius.repository.tax_rate" />
<argument type="service" id="sylius.tax_rate_date_eligibility_checker" on-invalid="null" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<argument type="service" id="sylius.tax_rate_date_eligibility_checker" on-invalid="null" />
<argument type="service" id="sylius.tax_rate_date_eligibility_checker" />

I guess it's not needed.

@Zales0123 Zales0123 merged commit 6e71ab4 into Sylius:1.12 Oct 10, 2022
@Zales0123
Copy link
Member

Thanks, @everwhatever! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Admin AdminBundle related issues and PRs. Feature New feature proposals. Maintenance CI configurations, READMEs, releases, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants