-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathCanadaTaxTypeResolver.php
72 lines (63 loc) · 2 KB
/
CanadaTaxTypeResolver.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
<?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;
/**
* Resolver for Canada's tax types (HST, PST, GST).
*/
class CanadaTaxTypeResolver implements TaxTypeResolverInterface
{
/**
* The tax type repository.
*
* @param TaxTypeRepositoryInterface
*/
protected $taxTypeRepository;
/**
* Creates a CanadaTaxTypeResolver instance.
*
* @param TaxTypeRepositoryInterface $taxTypeRepository The tax type repository.
*/
public function __construct(TaxTypeRepositoryInterface $taxTypeRepository)
{
$this->taxTypeRepository = $taxTypeRepository;
}
/**
* {@inheritdoc}
*/
public function resolve(TaxableInterface $taxable, Context $context)
{
$customerAddress = $context->getCustomerAddress();
$storeAddress = $context->getStoreAddress();
if ($customerAddress->getCountryCode() != 'CA' || $storeAddress->getCountryCode() != 'CA') {
// The customer or the store is not in Canada.
return [];
}
// Canadian tax types are matched by the customer address.
// If the customer is from Ontario, the tax types are for Ontario.
$taxTypes = $this->getTaxTypes();
$results = [];
foreach ($taxTypes as $taxType) {
$zone = $taxType->getZone();
if ($zone->match($customerAddress)) {
$results[] = $taxType;
}
}
return $results;
}
/**
* Returns the Canadian tax types.
*
* @return TaxTypeInterface[] An array of Canadian tax types.
*/
protected function getTaxTypes()
{
$taxTypes = $this->taxTypeRepository->getAll();
$taxTypes = array_filter($taxTypes, function ($taxType) {
return $taxType->getTag() == 'CA';
});
return $taxTypes;
}
}