Services
- What is the tax accountant service?
- What is the combination serializer service?
- What is the login blocker service?
- What is the volume service?
- What is the weight service
What is the tax accountant service?
The Tax Account is the service responsible for determining the tax multiplier that should be applied to a product price.
It uses the store's tax configuration together with the current customer's tax information and location to determine which tax rules and rates apply. The service can then use those multipliers to convert prices between tax-exclusive and tax-inclusive values.
Converting prices
Ex => Inc
The priceInc method can be used to convert an ex price to an inc price, we use it when handling prices for products:
app('aero.accountant')->priceInc($value, $this->product_tax_group_id);
Inc => Ex
The priceEx method can be used to convert an inc price to an ex price, we use it when handling prices for products:
app('aero.accountant')->priceEx($value, $this->product_tax_group_id);
Resolvers
Tax Group Resolver
The Tax Account uses a tax group resolver to resolve a customer's tax group id.
A resolver is set by the Aero\Store\Http\Middleware\SetTaxResolver middleware:
$this->accountant->setTaxGroupResolver(static function (?\Aero\Account\Models\Customer $customer) {
if ($customer && ($group = $customer->getAttribute('customer_tax_group_id'))) {
return $group;
}
if ($customer && $customer->customer_group_id && ($group = $customer->group->getAttribute('customer_tax_group_id'))) {
return $group;
}
return null;
});
If no customer tax group id is resolved, the store's default will be used.
Country Resolver
The Tax Account uses a country resolver to resolve the country it should use.
A resolver is set by the Aero\Store\Http\Middleware\SetTaxResolver middleware:
$this->accountant->setCountryResolver(static function (?\Aero\Account\Models\Customer $customer) {
if ($cartLocale = \Aero\Cart\Facades\Cart::instance(PaymentDriver::MOTO)->locale()) {
return $cartLocale->countryCode;
}
return null;
});
If no country code is resolved, the store's default will be used.
Zone Resolver
The Tax Account uses a country resolver to resolve the country it should use.
A resolver is set by the Aero\Store\Http\Middleware\SetTaxResolver middleware:
$this->accountant->setZoneResolver(static function (?\Aero\Account\Models\Customer $customer) {
if ($cartLocale = \Aero\Cart\Facades\Cart::instance(PaymentDriver::MOTO)->locale()) {
return $cartLocale->zoneId;
}
return null;
});
If no zone id is resolved, then no zone id will be used.
Postcode Resolver
The Tax Account uses a country resolver to resolve the country it should use.
A resolver is set by the Aero\Store\Http\Middleware\SetTaxResolver middleware:
$this->accountant->setPostcodeResolver(static function (?\Aero\Account\Models\Customer $customer) {
if ($cartLocale = \Aero\Cart\Facades\Cart::instance(PaymentDriver::MOTO)->locale()) {
return $cartLocale->postcode;
}
return null;
});
If no postcode is resolved, then no postcode will be used.
What is the combination serializer service?
The combination serializer service is used to serialize and deserialize models.
Serializing
This is used to take a collection of models and convert them into a string that can be used to find a corresponding combination.
Let's say you have a category called mens, a manufacturer called acme and a blue tag in the group color. Then if you visit /mens/acme/blue on the storefront it will load any products in the category mens that have the manufacturer acme and are tagged with blue.
Perhaps you might rather that it 404s unless a combination for those models exists (as otherwise there may very likely be no products that matches the criteria). This can be done by serializing the slugs and considering them invalid if they don't correspond to an existing combination model:
\Aero\Store\Routing\Slugs::setValidator(function (\Illuminate\Database\Eloquent\Collection $slugs, $resolver) {
if ($resolver !== 'listings') return true;
try {
$serialized = \Aero\Common\Services\CombinationSerializer::serialize(
$slugs->map(function (\Aero\Store\Models\Slug $slug) {
return $slug->model;
})
);
return \Aero\Catalog\Models\Combination::where('serialized', $serialized)->exists();
} catch (\Exception $e) {
return false;
}
});
Deserializing
This is used to take a combination model and find all of its' component models (collections, categories, manufacturers & tags).
For example, you might want to resolve the category ids for the listings page from the combination:
\Aero\Store\Http\Responses\ListingsPage::extend(function (\Aero\Store\Http\Responses\ListingsPage $page, \Closure $next) {
$categoryIds = \Aero\Common\Services\CombinationSerializer::deserialize($page->getData('combination'))
->filter(function (\Illuminate\Database\Eloquent\Model $model) {
return $model instanceof \Aero\Catalog\Models\Category;
})
->pluck('id');
// Do stuff...
return $next($page);
});
What is the login blocker service?
The login blocker service can be used to block customers from logging in based on criteria.
For example, you might want to blacklist certain IPs.
First, define a setting to store blacklisted IPs:
\Aero\Common\Facades\Settings::group('customers', static function (\Aero\Common\Settings\SettingGroup $group) {
$group->array('blocked_ip_addresses');
});
Then add a login blocker to check if the customer IP is blocked:
\Aero\Common\Facades\LoginBlocker::add(function (\Aero\Account\Models\Customer $customer) {
if (in_array(request()->ip(), setting('customers.blocked_ip_addresses')) {
return __('Blocked due to suspicious activity');
}
});
Returning a string means that they customer will be blocked from logging in and that string will be displayed.
What is the volume service?
The volume service is used to normalize volumes into the store's base unit (cm^3 by default) as well as convert between different units.
Units
By default the volume service has the following units:
mm^3cm^3m^3
Adding a Unit
Use addUnit() to add a unit, for example we can add millilitres & litres:
\Aero\Common\Facades\Volume::addUnit('ml', 'ml');
\Aero\Common\Facades\Volume::addUnit('l', 'L')
->setNormalizer(function ($value) {
return $value * 1000;
});
setNormalizer defines how to get from that unit to the base unit, which is cm^3.
Removing a Unit
\Aero\Common\Facades\Volume::removeUnit('m^3');
Converting between Units
You can use convert() to convert between any two units, or normalize() to convert into the store's base unit.
\Aero\Common\Facades\Volume::convert(1000, 'mm^3', 'cm^3');
\Aero\Common\Facades\Volume::normalize('mm^3', 1000);
Setting a base Unit
\Aero\Common\Facades\Volume::setBaseUnit('ml');
When you change base unit, database values will still remain in cm^3. But volumes will be displayed in the new unit on screens such as the fulfillment create page.
What is the weight service
The weight service is used to normalize weights into the store's base unit (g by default) as well as convert between different units.
Units
By default the weight service has the following units:
gozlbkg
Adding a Unit
Use addUnit() to add a unit, for example we can add milligrams:
\Aero\Common\Facades\Weight::addUnit('mg', 'mg')
->setNormalizer(function ($value) {
return $value / 1000;
});
setNormalizer defines how to get from that unit to the base unit, which is g.
Removing a Unit
\Aero\Common\Facades\Weight::removeUnit('kg');
Converting between Units
You can use convert() to convert between any two units, or normalize() to convert into the store's base unit.
\Aero\Common\Facades\Weight::convert(1000, 'oz', 'g');
\Aero\Common\Facades\Weight::normalize('oz', 1000);
Setting a base Unit
\Aero\Common\Facades\Weight::setBaseUnit('oz');
When you change base unit, database values will still remain in grams. But weights will be displayed in the new unit on screens such as the fulfillment create page.