-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathDefaultTaxTypeResolver.php
84 lines (74 loc) · 2.54 KB
/
DefaultTaxTypeResolver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace CommerceGuys\Tax\Resolver\TaxType;
use CommerceGuys\Tax\Model\TaxTypeInterface;
use CommerceGuys\Tax\TaxableInterface;
use CommerceGuys\Tax\Repository\TaxTypeRepositoryInterface;
use CommerceGuys\Tax\Resolver\Context;
/**
* Default resolver.
*
* Meant to run last in the process.
* A tax type applies if both the store and the customer are in the same zone
* (e.g. the customer is in Serbia and the store is in Serbia) OR the store is
* registered to collect taxes in the customer's zone (e.g. the store is in
* Serbia and the customer is in Montenegro, but the store is registered to
* collect Montenegrin VAT).
*/
class DefaultTaxTypeResolver implements TaxTypeResolverInterface
{
use StoreRegistrationCheckerTrait;
/**
* The tax type repository.
*
* @param TaxTypeRepositoryInterface
*/
protected $taxTypeRepository;
/**
* Creates a DefaultTaxTypeResolver instance.
*
* @param TaxTypeRepositoryInterface $taxTypeRepository The tax type repository.
*/
public function __construct(TaxTypeRepositoryInterface $taxTypeRepository)
{
$this->taxTypeRepository = $taxTypeRepository;
}
/**
* {@inheritdoc}
*/
public function resolve(TaxableInterface $taxable, Context $context)
{
$taxTypes = $this->getTaxTypes();
$results = [];
foreach ($taxTypes as $taxType) {
$zone = $taxType->getZone();
$customerZoneMatch = $zone->match($context->getCustomerAddress());
$storeZoneMatch = $zone->match($context->getStoreAddress());
if ($customerZoneMatch && $storeZoneMatch) {
// The customer and store belong to the same zone.
$results[] = $taxType;
} elseif ($customerZoneMatch && $this->checkStoreRegistration($zone, $context)) {
// The customer belongs to the zone, and the store is
// registered to collect taxes there.
$results[] = $taxType;
}
}
return $results;
}
/**
* Returns the non-tagged tax types.
*
* It is assumed that the tagged tax types have already been evaluated by
* other resolvers.
*
* @return TaxTypeInterface[] An array of non-tagged tax types.
*/
protected function getTaxTypes()
{
$taxTypes = $this->taxTypeRepository->getAll();
$taxTypes = array_filter($taxTypes, function ($taxType) {
$tag = $taxType->getTag();
return empty($tag);
});
return $taxTypes;
}
}